namespace com.hitrust.util.Checksums
|
|
{
|
|
using System;
|
|
|
|
public sealed class Adler32 : IChecksum
|
|
{
|
|
private static readonly uint BASE = 0xfff1;
|
|
private uint checksum;
|
|
|
|
public Adler32()
|
|
{
|
|
this.Reset();
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
this.checksum = 1;
|
|
}
|
|
|
|
public void Update(int bval)
|
|
{
|
|
uint s1 = this.checksum & 0xffff;
|
|
uint s2 = this.checksum >> 0x10;
|
|
s1 = (uint) ((s1 + (bval & 0xff)) % BASE);
|
|
s2 = (s1 + s2) % BASE;
|
|
this.checksum = (s2 << 0x10) + s1;
|
|
}
|
|
|
|
public void Update(byte[] buffer)
|
|
{
|
|
this.Update(buffer, 0, buffer.Length);
|
|
}
|
|
|
|
public void Update(byte[] buf, int off, int len)
|
|
{
|
|
if (buf == null)
|
|
{
|
|
throw new ArgumentNullException("buf");
|
|
}
|
|
if (((off < 0) || (len < 0)) || ((off + len) > buf.Length))
|
|
{
|
|
throw new ArgumentOutOfRangeException();
|
|
}
|
|
uint s1 = this.checksum & 0xffff;
|
|
uint s2 = this.checksum >> 0x10;
|
|
while (len > 0)
|
|
{
|
|
int n = 0xed8;
|
|
if (n > len)
|
|
{
|
|
n = len;
|
|
}
|
|
len -= n;
|
|
while (--n >= 0)
|
|
{
|
|
s1 += (uint) (buf[off++] & 0xff);
|
|
s2 += s1;
|
|
}
|
|
s1 = s1 % BASE;
|
|
s2 = s2 % BASE;
|
|
}
|
|
this.checksum = (s2 << 0x10) | s1;
|
|
}
|
|
|
|
public long Value
|
|
{
|
|
get
|
|
{
|
|
return (long) this.checksum;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|