Skip to content

Commit 391e4f8

Browse files
committed
Add Adler32
1 parent 0f6a5db commit 391e4f8

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace LibDeflate.Checksums
5+
{
6+
using static Imports.Checksums;
7+
8+
public struct Adler32
9+
{
10+
private bool _initialized;
11+
//because we have to supply 1 as the initial value
12+
//we must always retrieve this through Hash
13+
private uint _currentAdler;
14+
public uint Hash
15+
{
16+
get
17+
{
18+
if(!_initialized)
19+
{
20+
_currentAdler = 1;
21+
_initialized = true;
22+
}
23+
return _currentAdler;
24+
}
25+
}
26+
27+
public void Append(ReadOnlySpan<byte> input)
28+
=> _currentAdler = AppendCore(Hash, input);
29+
30+
public uint Compute(ReadOnlySpan<byte> input)
31+
=> _currentAdler = AppendCore(1, input);
32+
33+
private static uint AppendCore(uint adler, ReadOnlySpan<byte> input)
34+
=> libdeflate_adler32(adler, MemoryMarshal.GetReference(input), (nuint)input.Length);
35+
}
36+
}

0 commit comments

Comments
 (0)