Files
AaruBenchmark/Aaru6.Checksums/Adler32Context.cs

403 lines
12 KiB
C#
Raw Normal View History

2021-09-22 15:37:59 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Adler32Context.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Checksums.
//
// --[ Description ] ----------------------------------------------------------
//
// Implements an Adler-32 algorithm.
//
// --[ License ] --------------------------------------------------------------
//
2021-10-13 05:14:46 +01:00
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
2021-09-22 15:37:59 +01:00
//
2021-10-13 05:14:46 +01:00
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
2021-09-22 15:37:59 +01:00
//
2021-10-13 05:14:46 +01:00
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
2021-09-22 15:37:59 +01:00
//
// ----------------------------------------------------------------------------
2024-04-30 04:20:22 +01:00
// Copyright © 2011-2024 Natalia Portillo
2021-10-13 05:14:46 +01:00
// Copyright (C) 1995-2011 Mark Adler
// Copyright (C) Jean-loup Gailly
2021-09-22 15:37:59 +01:00
// ****************************************************************************/
2021-10-13 21:50:34 +01:00
using System;
2021-09-22 15:37:59 +01:00
using System.IO;
2021-10-13 21:50:34 +01:00
using System.Runtime.InteropServices;
2021-09-29 01:21:30 +01:00
using System.Runtime.Intrinsics.Arm;
2021-09-28 22:34:29 +01:00
using System.Runtime.Intrinsics.X86;
2021-09-22 15:37:59 +01:00
using System.Text;
using Aaru.CommonTypes.Interfaces;
using Aaru.Helpers;
2021-09-28 22:34:29 +01:00
using Aaru6.Checksums.Adler32;
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
namespace Aaru6.Checksums;
/// <inheritdoc />
/// <summary>Implements the Adler-32 algorithm</summary>
public sealed class Adler32Context : IChecksum
2021-09-22 15:37:59 +01:00
{
2022-12-02 15:33:35 +00:00
internal const ushort ADLER_MODULE = 65521;
internal const uint NMAX = 5552;
readonly IntPtr _nativeContext;
readonly bool _useNative;
ushort _sum1, _sum2;
/// <summary>Initializes the Adler-32 sums</summary>
public Adler32Context()
2021-09-22 15:37:59 +01:00
{
2022-12-02 15:33:35 +00:00
_sum1 = 1;
_sum2 = 0;
2021-10-13 21:50:34 +01:00
2024-04-30 04:30:11 +01:00
if(!Native.IsSupported) return;
2021-10-13 21:50:34 +01:00
2022-12-02 15:33:35 +00:00
_nativeContext = adler32_init();
_useNative = _nativeContext != IntPtr.Zero;
}
2021-10-13 21:50:34 +01:00
2022-12-02 15:33:35 +00:00
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern IntPtr adler32_init();
2021-10-13 21:50:34 +01:00
2022-12-02 15:33:35 +00:00
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int adler32_update(IntPtr ctx, byte[] data, uint len);
2021-10-13 21:50:34 +01:00
2022-12-02 15:33:35 +00:00
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern int adler32_final(IntPtr ctx, ref uint checksum);
2021-09-28 22:34:29 +01:00
2022-12-02 15:33:35 +00:00
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
static extern void adler32_free(IntPtr ctx);
2021-09-28 22:34:29 +01:00
2022-12-02 15:33:35 +00:00
static void Step(ref ushort preSum1, ref ushort preSum2, byte[] data, uint len, bool useNative,
2023-10-03 21:49:17 +01:00
IntPtr nativeContext)
2022-12-02 15:33:35 +00:00
{
if(useNative)
{
adler32_update(nativeContext, data, len);
2021-09-29 01:21:30 +01:00
2022-12-02 15:33:35 +00:00
return;
}
2021-09-29 01:21:30 +01:00
2022-12-02 15:33:35 +00:00
if(Ssse3.IsSupported)
{
ssse3.Step(ref preSum1, ref preSum2, data, len);
2022-12-02 15:33:35 +00:00
return;
}
2022-12-02 15:33:35 +00:00
if(AdvSimd.IsSupported)
{
neon.Step(ref preSum1, ref preSum2, data, len);
2022-12-02 15:33:35 +00:00
return;
}
2022-12-02 15:33:35 +00:00
uint sum1 = preSum1;
uint sum2 = preSum2;
uint n;
2023-10-03 21:49:17 +01:00
var dataOff = 0;
2022-12-02 15:33:35 +00:00
/* in case user likes doing a byte at a time, keep it fast */
if(len == 1)
{
sum1 += data[dataOff];
2024-04-30 04:30:11 +01:00
if(sum1 >= ADLER_MODULE) sum1 -= ADLER_MODULE;
2022-12-02 15:33:35 +00:00
sum2 += sum1;
2024-04-30 04:30:11 +01:00
if(sum2 >= ADLER_MODULE) sum2 -= ADLER_MODULE;
2022-12-02 15:33:35 +00:00
preSum1 = (ushort)(sum1 & 0xFFFF);
preSum2 = (ushort)(sum2 & 0xFFFF);
2022-12-02 15:33:35 +00:00
return;
}
2022-12-02 15:33:35 +00:00
/* in case short lengths are provided, keep it somewhat fast */
if(len < 16)
{
while(len-- > 0)
{
2022-12-02 15:33:35 +00:00
sum1 += data[dataOff++];
sum2 += sum1;
}
2024-04-30 04:30:11 +01:00
if(sum1 >= ADLER_MODULE) sum1 -= ADLER_MODULE;
2022-12-02 15:33:35 +00:00
sum2 %= ADLER_MODULE; /* only added so many ADLER_MODULE's */
preSum1 = (ushort)(sum1 & 0xFFFF);
preSum2 = (ushort)(sum2 & 0xFFFF);
return;
2021-09-22 15:37:59 +01:00
}
2022-12-02 15:33:35 +00:00
/* do length NMAX blocks -- requires just one modulo operation */
while(len >= NMAX)
2021-09-22 15:37:59 +01:00
{
2022-12-02 15:33:35 +00:00
len -= NMAX;
n = NMAX / 16; /* NMAX is divisible by 16 */
do
{
sum1 += data[dataOff];
sum2 += sum1;
sum1 += data[dataOff + 1];
sum2 += sum1;
sum1 += data[dataOff + 2];
sum2 += sum1;
sum1 += data[dataOff + 2 + 1];
sum2 += sum1;
sum1 += data[dataOff + 4];
sum2 += sum1;
sum1 += data[dataOff + 4 + 1];
sum2 += sum1;
sum1 += data[dataOff + 4 + 2];
sum2 += sum1;
sum1 += data[dataOff + 4 + 2 + 1];
sum2 += sum1;
sum1 += data[dataOff + 8];
sum2 += sum1;
sum1 += data[dataOff + 8 + 1];
sum2 += sum1;
sum1 += data[dataOff + 8 + 2];
sum2 += sum1;
sum1 += data[dataOff + 8 + 2 + 1];
sum2 += sum1;
sum1 += data[dataOff + 8 + 4];
sum2 += sum1;
sum1 += data[dataOff + 8 + 4 + 1];
sum2 += sum1;
sum1 += data[dataOff + 8 + 4 + 2];
sum2 += sum1;
sum1 += data[dataOff + 8 + 4 + 2 + 1];
sum2 += sum1;
/* 16 sums unrolled */
dataOff += 16;
} while(--n != 0);
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
sum1 %= ADLER_MODULE;
sum2 %= ADLER_MODULE;
2021-09-22 15:37:59 +01:00
}
2022-12-02 15:33:35 +00:00
/* do remaining bytes (less than NMAX, still just one modulo) */
if(len != 0)
2021-09-22 15:37:59 +01:00
{
2022-12-02 15:33:35 +00:00
/* avoid modulos if none remaining */
while(len >= 16)
2021-10-13 21:50:34 +01:00
{
2022-12-02 15:33:35 +00:00
len -= 16;
sum1 += data[dataOff];
sum2 += sum1;
sum1 += data[dataOff + 1];
sum2 += sum1;
sum1 += data[dataOff + 2];
sum2 += sum1;
sum1 += data[dataOff + 2 + 1];
sum2 += sum1;
sum1 += data[dataOff + 4];
sum2 += sum1;
sum1 += data[dataOff + 4 + 1];
sum2 += sum1;
sum1 += data[dataOff + 4 + 2];
sum2 += sum1;
sum1 += data[dataOff + 4 + 2 + 1];
sum2 += sum1;
sum1 += data[dataOff + 8];
sum2 += sum1;
sum1 += data[dataOff + 8 + 1];
sum2 += sum1;
sum1 += data[dataOff + 8 + 2];
sum2 += sum1;
sum1 += data[dataOff + 8 + 2 + 1];
sum2 += sum1;
sum1 += data[dataOff + 8 + 4];
sum2 += sum1;
sum1 += data[dataOff + 8 + 4 + 1];
sum2 += sum1;
sum1 += data[dataOff + 8 + 4 + 2];
sum2 += sum1;
sum1 += data[dataOff + 8 + 4 + 2 + 1];
sum2 += sum1;
2021-10-13 21:50:34 +01:00
2022-12-02 15:33:35 +00:00
dataOff += 16;
2021-10-13 21:50:34 +01:00
}
2022-12-02 15:33:35 +00:00
while(len-- != 0)
{
sum1 += data[dataOff++];
sum2 += sum1;
}
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
sum1 %= ADLER_MODULE;
sum2 %= ADLER_MODULE;
}
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
preSum1 = (ushort)(sum1 & 0xFFFF);
preSum2 = (ushort)(sum2 & 0xFFFF);
}
2022-12-02 15:33:35 +00:00
/// <summary>Gets the hash of a file</summary>
/// <param name="filename">File path.</param>
public static byte[] File(string filename)
{
File(filename, out byte[] hash);
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
return hash;
}
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
/// <summary>Gets the hash of a file in hexadecimal and as a byte array.</summary>
/// <param name="filename">File path.</param>
/// <param name="hash">Byte array of the hash value.</param>
public static string File(string filename, out byte[] hash)
{
bool useNative = Native.IsSupported;
IntPtr nativeContext = IntPtr.Zero;
if(useNative)
{
nativeContext = adler32_init();
2021-10-13 21:50:34 +01:00
2024-04-30 04:30:11 +01:00
if(nativeContext == IntPtr.Zero) useNative = false;
2022-12-02 15:33:35 +00:00
}
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
var fileStream = new FileStream(filename, FileMode.Open);
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
ushort localSum1 = 1;
ushort localSum2 = 0;
2021-09-22 15:37:59 +01:00
2023-10-03 21:49:17 +01:00
var buffer = new byte[65536];
int read = fileStream.Read(buffer, 0, 65536);
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
while(read > 0)
{
Step(ref localSum1, ref localSum2, buffer, (uint)read, useNative, nativeContext);
read = fileStream.Read(buffer, 0, 65536);
2021-09-22 15:37:59 +01:00
}
2023-10-03 21:49:17 +01:00
var finalSum = (uint)(localSum2 << 16 | localSum1);
2022-12-02 15:33:35 +00:00
if(useNative)
2021-09-22 15:37:59 +01:00
{
2022-12-02 15:33:35 +00:00
adler32_final(nativeContext, ref finalSum);
adler32_free(nativeContext);
}
2021-10-13 21:50:34 +01:00
2022-12-02 15:33:35 +00:00
hash = BigEndianBitConverter.GetBytes(finalSum);
2021-10-13 21:50:34 +01:00
2022-12-02 15:33:35 +00:00
var adlerOutput = new StringBuilder();
2021-10-13 21:50:34 +01:00
2024-04-30 04:30:11 +01:00
foreach(byte h in hash) adlerOutput.Append(h.ToString("x2"));
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
fileStream.Close();
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
return adlerOutput.ToString();
}
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
/// <summary>Gets the hash of the specified data buffer.</summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of the data buffer to hash.</param>
/// <param name="hash">Byte array of the hash value.</param>
public static string Data(byte[] data, uint len, out byte[] hash)
{
bool useNative = Native.IsSupported;
IntPtr nativeContext = IntPtr.Zero;
2021-10-13 21:50:34 +01:00
2022-12-02 15:33:35 +00:00
if(useNative)
{
nativeContext = adler32_init();
2021-09-22 15:37:59 +01:00
2024-04-30 04:30:11 +01:00
if(nativeContext == IntPtr.Zero) useNative = false;
2022-12-02 15:33:35 +00:00
}
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
ushort localSum1 = 1;
ushort localSum2 = 0;
2021-09-22 15:37:59 +01:00
2022-12-02 15:33:35 +00:00
Step(ref localSum1, ref localSum2, data, len, useNative, nativeContext);
2023-10-03 21:49:17 +01:00
var finalSum = (uint)(localSum2 << 16 | localSum1);
2022-12-02 15:33:35 +00:00
if(useNative)
{
adler32_final(nativeContext, ref finalSum);
adler32_free(nativeContext);
2021-09-22 15:37:59 +01:00
}
2022-12-02 15:33:35 +00:00
hash = BigEndianBitConverter.GetBytes(finalSum);
var adlerOutput = new StringBuilder();
2024-04-30 04:30:11 +01:00
foreach(byte h in hash) adlerOutput.Append(h.ToString("x2"));
2022-12-02 15:33:35 +00:00
return adlerOutput.ToString();
2021-09-22 15:37:59 +01:00
}
2022-12-02 15:33:35 +00:00
/// <summary>Gets the hash of the specified data buffer.</summary>
/// <param name="data">Data buffer.</param>
/// <param name="hash">Byte array of the hash value.</param>
public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash);
2024-04-30 04:30:11 +01:00
#region IChecksum Members
/// <inheritdoc />
/// <summary>Updates the hash with data.</summary>
/// <param name="data">Data buffer.</param>
/// <param name="len">Length of buffer to hash.</param>
public void Update(byte[] data, uint len) => Step(ref _sum1, ref _sum2, data, len, _useNative, _nativeContext);
/// <inheritdoc />
/// <summary>Updates the hash with data.</summary>
/// <param name="data">Data buffer.</param>
public void Update(byte[] data) => Update(data, (uint)data.Length);
/// <inheritdoc />
/// <summary>Returns a byte array of the hash value.</summary>
public byte[] Final()
{
var finalSum = (uint)(_sum2 << 16 | _sum1);
if(!_useNative) return BigEndianBitConverter.GetBytes(finalSum);
adler32_final(_nativeContext, ref finalSum);
adler32_free(_nativeContext);
return BigEndianBitConverter.GetBytes(finalSum);
}
/// <inheritdoc />
/// <summary>Returns a hexadecimal representation of the hash value.</summary>
public string End()
{
var finalSum = (uint)(_sum2 << 16 | _sum1);
if(_useNative)
{
adler32_final(_nativeContext, ref finalSum);
adler32_free(_nativeContext);
}
var adlerOutput = new StringBuilder();
for(var i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
adlerOutput.Append(BigEndianBitConverter.GetBytes(finalSum)[i].ToString("x2"));
return adlerOutput.ToString();
}
#endregion
2021-09-22 15:37:59 +01:00
}