Skip to content

Commit

Permalink
Simplify Adler32
Browse files Browse the repository at this point in the history
  • Loading branch information
seionmoya committed Sep 25, 2024
1 parent 094cc46 commit 17d3b9a
Showing 1 changed file with 15 additions and 40 deletions.
55 changes: 15 additions & 40 deletions Zlib.Managed/Elskom/Adler32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,38 @@ internal static class Adler32
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private const int NMAX = 5552;

internal static long Calculate(long adler, ReadOnlySpan<byte> buf, int index, int len)
public static long Calculate(long adler, ReadOnlySpan<byte> buffer, int index, int length)
{
if (buf == null)
if (buffer == null)
{
return 1L;
}

var s1 = adler & 0xffff;
var s2 = (adler >> 16) & 0xffff;
var s1 = adler & 0xFFFF;
var s2 = (adler >> 16) & 0xFFFF;
int k;

while (len > 0)
while (length > 0)
{
k = len < NMAX ? len : NMAX;
len -= k;
k = length < NMAX ? length : NMAX;
length -= k;

while (k >= 16)
{
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
s1 += buf[index++] & 0xff;
s2 += s1;
for (var i = 0; i < 16; ++i)
{
s1 += buffer[index++] & 0xFF;
s2 += s1;
}

k -= 16;
}

if (k != 0)
{
do
{
s1 += buf[index++] & 0xff;
s1 += buffer[index++] & 0xFF;
s2 += s1;
}
while (--k != 0);
Expand Down

0 comments on commit 17d3b9a

Please sign in to comment.