Skip to content

Commit 9255470

Browse files
committed
Add xxHash state classes
1 parent c5e4cb1 commit 9255470

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

SabreTools.Hashing/XxHash/Constants.cs

+13
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ internal static class Constants
5656

5757
public const ulong PRIME_MX2 = 0x9FB21C651E98DF25;
5858

59+
/// <summary>
60+
/// The size of the internal XXH3 buffer.
61+
/// This is the optimal update size for incremental hashing.
62+
/// </summary>
63+
public const int XXH3_INTERNALBUFFER_SIZE = 256;
64+
65+
/// <summary>
66+
/// Default size of the secret buffer (and <see cref="XXH3_kSecret"/>).
67+
/// This is the size used in <see cref="XXH3_kSecret"/> and the seeded functions.
68+
/// Not to be confused with @ref XXH3_SECRET_SIZE_MIN.
69+
/// </summary>
70+
public const int XXH3_SECRET_DEFAULT_SIZE = 192;
71+
5972
#endregion
6073
}
6174
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace SabreTools.Hashing.XxHash
2+
{
3+
/// <summary>
4+
/// Structure for XXH32 streaming API.
5+
/// </summary>
6+
/// <see href="https://github.com/Cyan4973/xxHash/blob/dev/xxhash.h"/>
7+
internal class XXH32State
8+
{
9+
/// <summary>
10+
/// Total length hashed, modulo 2^32
11+
/// </summary>
12+
public uint TotalLength { get; set; }
13+
14+
/// <summary>
15+
/// Whether the hash is >= 16 (handles @ref total_len_32 overflow)
16+
/// </summary>
17+
public uint LargeLength { get; set; }
18+
19+
/// <summary>
20+
/// Accumulator lanes
21+
/// </summary>
22+
public uint[] AccumulatorLanes { get; } = new uint[4];
23+
24+
/// <summary>
25+
/// Internal buffer for partial reads. Treated as unsigned char[16].
26+
/// </summary>
27+
public uint[] PartialReadBuffer { get; } = new uint[4];
28+
29+
/// <summary>
30+
/// Amount of data in <see cref="PartialReadBuffer">
31+
/// </summary>
32+
public uint Memsize { get; set; }
33+
34+
/// <summary>
35+
/// Reserved field. Do not read nor write to it.
36+
/// </summary>
37+
public uint Reserved { get; set; }
38+
}
39+
}
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace SabreTools.Hashing.XxHash
2+
{
3+
/// <summary>
4+
/// Structure for XXH3 streaming API.
5+
/// </summary>
6+
/// <see href="https://github.com/Cyan4973/xxHash/blob/dev/xxhash.h"/>
7+
internal class XXH3State
8+
{
9+
/// <summary>
10+
/// Accumulator lanes
11+
/// </summary>
12+
public ulong[] AccumulatorLanes { get; } = new ulong[8];
13+
14+
/// <summary>
15+
/// Used to store a custom secret generated from a seed.
16+
/// </summary>
17+
public byte[] CustomSecret { get; } = new byte[Constants.XXH3_SECRET_DEFAULT_SIZE];
18+
19+
/// <summary>
20+
/// The internal buffer. <see cref="XXH32State.PartialReadBuffer"/>
21+
/// </summary>
22+
public byte[] Buffer { get; } = new byte[Constants.XXH3_INTERNALBUFFER_SIZE];
23+
24+
/// <summary>
25+
/// The amount of memory in <see cref="Buffer"/>, <see cref="XXH32State.Memsize"/>
26+
/// </summary>
27+
public uint BufferedSize { get; set; }
28+
29+
/// <summary>
30+
/// Reserved field. Needed for padding on 64-bit.
31+
/// </summary>
32+
public uint UseSeed { get; set; }
33+
34+
/// <summary>
35+
/// Number or stripes processed.
36+
/// </summary>
37+
public ulong StripesSoFar { get; set; }
38+
39+
/// <summary>
40+
/// Total length hashed. 64-bit even on 32-bit targets.
41+
/// </summary>
42+
public ulong TotalLength { get; set; }
43+
44+
/// <summary>
45+
/// Number of stripes per block.
46+
/// </summary>
47+
public ulong StripesPerBlock { get; set; }
48+
49+
/// <summary>
50+
/// Size of <see cref="CustomSecret"/> or <see cref="ExtSecret">
51+
/// </summary>
52+
public ulong SecretLimit { get; set; }
53+
54+
/// <summary>
55+
/// Seed for _withSeed variants. Must be zero otherwise, @see XXH3_INITSTATE()
56+
/// </summary>
57+
public ulong Seed { get; set; }
58+
59+
/// <summary>
60+
/// Reserved field.
61+
/// </summary>
62+
public ulong Reserved64 { get; set; }
63+
64+
/// <summary>
65+
/// Reference to an external secret for the _withSecret variants, NULL
66+
/// for other variants.
67+
/// </summary>
68+
/// <remarks>There may be some padding at the end due to alignment on 64 bytes</remarks>
69+
public byte[]? ExtSecret { get; set; }
70+
}
71+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace SabreTools.Hashing.XxHash
2+
{
3+
/// <summary>
4+
/// Structure for XXH64 streaming API.
5+
/// </summary>
6+
/// <see href="https://github.com/Cyan4973/xxHash/blob/dev/xxhash.h"/>
7+
internal class XXH64State
8+
{
9+
/// <summary>
10+
/// Total length hashed. This is always 64-bit.
11+
/// </summary>
12+
public ulong TotalLength { get; set; }
13+
14+
/// <summary>
15+
/// Accumulator lanes
16+
/// </summary>
17+
public ulong[] AccumulatorLanes { get; } = new ulong[4];
18+
19+
/// <summary>
20+
/// Internal buffer for partial reads. Treated as unsigned char[16].
21+
/// </summary>
22+
public ulong[] PartialReadBuffer { get; } = new ulong[4];
23+
24+
/// <summary>
25+
/// Amount of data in <see cref="PartialReadBuffer">
26+
/// </summary>
27+
public uint Memsize { get; set; }
28+
29+
/// <summary>
30+
/// Reserved field, needed for padding anyways
31+
/// </summary>
32+
public uint Reserved32 { get; set; }
33+
34+
/// <summary>
35+
/// Reserved field. Do not read nor write to it.
36+
/// </summary>
37+
public ulong Reserved64 { get; set; }
38+
}
39+
}

0 commit comments

Comments
 (0)