Unify read names with IO

This commit is contained in:
Matt Nadareski
2026-03-22 02:29:17 -04:00
parent 62544b726d
commit f2499d9ae1
10 changed files with 83 additions and 111 deletions

View File

@@ -50,7 +50,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round
@@ -71,7 +71,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round

View File

@@ -51,7 +51,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round
@@ -72,7 +72,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round

View File

@@ -52,7 +52,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round
@@ -73,7 +73,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round

View File

@@ -55,7 +55,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round
@@ -76,7 +76,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round

View File

@@ -57,7 +57,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round
@@ -78,7 +78,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 16; i++)
{
_block[i] = ReadLE32(_buffer, i * 4);
_block[i] = ToUInt32LittleEndian(_buffer, i * 4);
}
// Run the round

View File

@@ -56,7 +56,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 8; i++)
{
_block[i] = ReadLE64(_buffer, i * 8);
_block[i] = ToUInt64LittleEndian(_buffer, i * 8);
}
// Run the round
@@ -77,7 +77,7 @@ namespace SabreTools.Hashing.CryptographicHash
// Split the buffer for the round
for (int i = 0; i < 8; i++)
{
_block[i] = ReadLE64(_buffer, i * 8);
_block[i] = ToUInt64LittleEndian(_buffer, i * 8);
}
// Run the round

View File

@@ -29,27 +29,6 @@ namespace SabreTools.Hashing
}
}
/// <summary>
/// Convert a byte array to a UInt64
/// </summary>
/// <param name="bytes">Byte array to convert</param>
/// <returns>UInt64 representing the byte array</returns>
/// <link>https://stackoverflow.com/questions/66750224/how-to-convert-a-byte-array-of-any-size-to-ulong-in-c</link>
public static ulong BytesToUInt64(byte[]? bytes)
{
// If we get null in, we send 0 out
if (bytes is null)
return default;
ulong result = 0;
for (int i = 0; i < bytes.Length; i++)
{
result |= (ulong)bytes[i] << (i * 8);
}
return result;
}
#if NET7_0_OR_GREATER
/// <summary>
/// Convert a byte array to a UInt64
@@ -69,68 +48,61 @@ namespace SabreTools.Hashing
result |= (UInt128)bytes[i] << (i * 8);
}
return result;
}
#else
/// <summary>
/// Convert a byte array to a UInt64
/// </summary>
/// <param name="bytes">Byte array to convert</param>
/// <returns>UInt64 representing the byte array</returns>
/// <link>https://stackoverflow.com/questions/66750224/how-to-convert-a-byte-array-of-any-size-to-ulong-in-c</link>
public static ulong BytesToUInt64(byte[]? bytes)
{
// If we get null in, we send 0 out
if (bytes is null)
return default;
ulong result = 0;
for (int i = 0; i < bytes.Length; i++)
{
result |= (ulong)bytes[i] << (i * 8);
}
return result;
}
#endif
#endregion
#region Read Big-Endian
/// <summary>
/// 32-bit big-endian read
/// </summary>
public static uint ReadBE32(byte[] data, int offset)
{
return (uint)(data[offset + 3]
| (data[offset + 2] << 8)
| (data[offset + 1] << 16)
| (data[offset + 0] << 24));
}
/// <summary>
/// 64-bit big-endian read
/// </summary>
public static ulong ReadBE64(byte[] data, int offset)
{
return data[offset + 7]
| ((ulong)data[offset + 6] << 8)
| ((ulong)data[offset + 5] << 16)
| ((ulong)data[offset + 4] << 24)
| ((ulong)data[offset + 3] << 32)
| ((ulong)data[offset + 2] << 40)
| ((ulong)data[offset + 1] << 48)
| ((ulong)data[offset + 0] << 56);
}
#endregion
#region Read Litte-Endian
/// <summary>
/// 32-bit little-endian read
/// Convert a byte array at an offset to a UInt32
/// </summary>
public static uint ReadLE32(byte[] data, int offset)
/// <remarks>Reads in little-endian format</remarks>
public static uint ToUInt32LittleEndian(byte[] value, int offset)
{
return (uint)(data[offset + 0]
| (data[offset + 1] << 8)
| (data[offset + 2] << 16)
| (data[offset + 3] << 24));
return (uint)(value[offset + 0]
| (value[offset + 1] << 8)
| (value[offset + 2] << 16)
| (value[offset + 3] << 24));
}
/// <summary>
/// 64-bit little-endian read
/// Convert a byte array at an offset to a UInt64
/// </summary>
public static ulong ReadLE64(byte[] data, int offset)
/// <remarks>Reads in little-endian format</remarks>
public static ulong ToUInt64LittleEndian(byte[] value, int offset)
{
return data[offset + 0]
| ((ulong)data[offset + 1] << 8)
| ((ulong)data[offset + 2] << 16)
| ((ulong)data[offset + 3] << 24)
| ((ulong)data[offset + 4] << 32)
| ((ulong)data[offset + 5] << 40)
| ((ulong)data[offset + 6] << 48)
| ((ulong)data[offset + 7] << 56);
return value[offset + 0]
| ((ulong)value[offset + 1] << 8)
| ((ulong)value[offset + 2] << 16)
| ((ulong)value[offset + 3] << 24)
| ((ulong)value[offset + 4] << 32)
| ((ulong)value[offset + 5] << 40)
| ((ulong)value[offset + 6] << 48)
| ((ulong)value[offset + 7] << 56);
}
#endregion

View File

@@ -83,10 +83,10 @@ namespace SabreTools.Hashing.NonCryptographicHash
Array.Copy(data, offset, _mem32, _memsize, 16 - _memsize);
int p32 = 0;
_acc[0] = Round(_acc[0], ReadLE32(_mem32, p32)); p32 += 4;
_acc[1] = Round(_acc[1], ReadLE32(_mem32, p32)); p32 += 4;
_acc[2] = Round(_acc[2], ReadLE32(_mem32, p32)); p32 += 4;
_acc[3] = Round(_acc[3], ReadLE32(_mem32, p32));
_acc[0] = Round(_acc[0], ToUInt32LittleEndian(_mem32, p32)); p32 += 4;
_acc[1] = Round(_acc[1], ToUInt32LittleEndian(_mem32, p32)); p32 += 4;
_acc[2] = Round(_acc[2], ToUInt32LittleEndian(_mem32, p32)); p32 += 4;
_acc[3] = Round(_acc[3], ToUInt32LittleEndian(_mem32, p32));
offset += 16 - _memsize;
_memsize = 0;
@@ -97,10 +97,10 @@ namespace SabreTools.Hashing.NonCryptographicHash
int limit = bEnd - 16;
do
{
_acc[0] = Round(_acc[0], ReadLE32(data, offset)); offset += 4;
_acc[1] = Round(_acc[1], ReadLE32(data, offset)); offset += 4;
_acc[2] = Round(_acc[2], ReadLE32(data, offset)); offset += 4;
_acc[3] = Round(_acc[3], ReadLE32(data, offset)); offset += 4;
_acc[0] = Round(_acc[0], ToUInt32LittleEndian(data, offset)); offset += 4;
_acc[1] = Round(_acc[1], ToUInt32LittleEndian(data, offset)); offset += 4;
_acc[2] = Round(_acc[2], ToUInt32LittleEndian(data, offset)); offset += 4;
_acc[3] = Round(_acc[3], ToUInt32LittleEndian(data, offset)); offset += 4;
} while (offset <= limit);
}
@@ -185,7 +185,7 @@ namespace SabreTools.Hashing.NonCryptographicHash
length &= 15;
while (length >= 4)
{
hash += ReadLE32(data, offset) * XXH_PRIME32_3;
hash += ToUInt32LittleEndian(data, offset) * XXH_PRIME32_3;
offset += 4;
hash = RotateLeft32(hash, 17) * XXH_PRIME32_4;
length -= 4;

View File

@@ -76,10 +76,10 @@ namespace SabreTools.Hashing.NonCryptographicHash
Array.Copy(data, offset, _mem64, _memsize, 32 - _memsize);
int p64 = 0;
_acc[0] = Round(_acc[0], ReadLE64(_mem64, p64)); p64 += 8;
_acc[1] = Round(_acc[1], ReadLE64(_mem64, p64)); p64 += 8;
_acc[2] = Round(_acc[2], ReadLE64(_mem64, p64)); p64 += 8;
_acc[3] = Round(_acc[3], ReadLE64(_mem64, p64));
_acc[0] = Round(_acc[0], ToUInt64LittleEndian(_mem64, p64)); p64 += 8;
_acc[1] = Round(_acc[1], ToUInt64LittleEndian(_mem64, p64)); p64 += 8;
_acc[2] = Round(_acc[2], ToUInt64LittleEndian(_mem64, p64)); p64 += 8;
_acc[3] = Round(_acc[3], ToUInt64LittleEndian(_mem64, p64));
offset += 32 - _memsize;
_memsize = 0;
@@ -90,10 +90,10 @@ namespace SabreTools.Hashing.NonCryptographicHash
int limit = bEnd - 32;
do
{
_acc[0] = Round(_acc[0], ReadLE64(data, offset)); offset += 8;
_acc[1] = Round(_acc[1], ReadLE64(data, offset)); offset += 8;
_acc[2] = Round(_acc[2], ReadLE64(data, offset)); offset += 8;
_acc[3] = Round(_acc[3], ReadLE64(data, offset)); offset += 8;
_acc[0] = Round(_acc[0], ToUInt64LittleEndian(data, offset)); offset += 8;
_acc[1] = Round(_acc[1], ToUInt64LittleEndian(data, offset)); offset += 8;
_acc[2] = Round(_acc[2], ToUInt64LittleEndian(data, offset)); offset += 8;
_acc[3] = Round(_acc[3], ToUInt64LittleEndian(data, offset)); offset += 8;
} while (offset <= limit);
}
@@ -175,7 +175,7 @@ namespace SabreTools.Hashing.NonCryptographicHash
length &= 31;
while (length >= 8)
{
ulong k1 = Round(0, ReadLE64(data, offset));
ulong k1 = Round(0, ToUInt64LittleEndian(data, offset));
offset += 8;
hash ^= k1;
hash = (RotateLeft64(hash, 27) * XXH_PRIME64_1) + XXH_PRIME64_4;
@@ -184,7 +184,7 @@ namespace SabreTools.Hashing.NonCryptographicHash
if (length >= 4)
{
hash ^= ReadLE32(data, offset) * XXH_PRIME64_1;
hash ^= ToUInt32LittleEndian(data, offset) * XXH_PRIME64_1;
offset += 4;
hash = (RotateLeft64(hash, 23) * XXH_PRIME64_2) + XXH_PRIME64_3;
length -= 4;

View File

@@ -121,7 +121,7 @@ namespace SabreTools.Hashing.XxHash
| ((uint)c2 << 24)
| ((uint)c3 << 0)
| ((uint)length << 8);
ulong bitflip = (ReadLE32(secret, 0) ^ ReadLE32(secret, 4)) + seed;
ulong bitflip = (ToUInt32LittleEndian(secret, 0) ^ ToUInt32LittleEndian(secret, 4)) + seed;
ulong keyed = combined ^ bitflip;
return XXH64Avalanche(keyed);
@@ -134,9 +134,9 @@ namespace SabreTools.Hashing.XxHash
{
seed ^= (ulong)Swap32((uint)seed) << 32;
uint input1 = ReadLE32(data, offset);
uint input2 = ReadLE32(data, offset + length - 4);
ulong bitflip = (ReadLE64(secret, 8) ^ ReadLE64(secret, 16)) - seed;
uint input1 = ToUInt32LittleEndian(data, offset);
uint input2 = ToUInt32LittleEndian(data, offset + length - 4);
ulong bitflip = (ToUInt64LittleEndian(secret, 8) ^ ToUInt64LittleEndian(secret, 16)) - seed;
ulong input64 = input2 + (((ulong)input1) << 32);
ulong keyed = input64 ^ bitflip;
@@ -148,10 +148,10 @@ namespace SabreTools.Hashing.XxHash
/// </summary>
public static ulong Len9To16Out64(byte[] data, int offset, int length, byte[] secret, ulong seed)
{
ulong bitflip1 = (ReadLE64(secret, 24) ^ ReadLE64(secret, 32)) + seed;
ulong bitflip2 = (ReadLE64(secret, 40) ^ ReadLE64(secret, 48)) - seed;
ulong input_lo = ReadLE64(data, offset) ^ bitflip1;
ulong input_hi = ReadLE64(data, offset + length - 8) ^ bitflip2;
ulong bitflip1 = (ToUInt64LittleEndian(secret, 24) ^ ToUInt64LittleEndian(secret, 32)) + seed;
ulong bitflip2 = (ToUInt64LittleEndian(secret, 40) ^ ToUInt64LittleEndian(secret, 48)) - seed;
ulong input_lo = ToUInt64LittleEndian(data, offset) ^ bitflip1;
ulong input_hi = ToUInt64LittleEndian(data, offset + length - 8) ^ bitflip2;
ulong acc = (ulong)length
+ Swap64(input_lo) + input_hi
@@ -172,17 +172,17 @@ namespace SabreTools.Hashing.XxHash
if (length > 0)
return Len1To3Out64(data, offset, length, secret, seed);
return XXH64Avalanche(seed ^ ReadLE64(secret, 56) ^ ReadLE64(secret, 64));
return XXH64Avalanche(seed ^ ToUInt64LittleEndian(secret, 56) ^ ToUInt64LittleEndian(secret, 64));
}
public static ulong Mix16B(byte[] data, int offset, byte[] secret, int secretOffset, ulong seed)
{
ulong input_lo = ReadLE64(data, offset + 0);
ulong input_hi = ReadLE64(data, offset + 8);
ulong input_lo = ToUInt64LittleEndian(data, offset + 0);
ulong input_hi = ToUInt64LittleEndian(data, offset + 8);
return MultiplyTo128Fold64(
input_lo ^ (ReadLE64(secret, secretOffset + 0) + seed),
input_hi ^ (ReadLE64(secret, secretOffset + 8) - seed)
input_lo ^ (ToUInt64LittleEndian(secret, secretOffset + 0) + seed),
input_hi ^ (ToUInt64LittleEndian(secret, secretOffset + 8) - seed)
);
}