4
adler32.c -- compute the Adler-32 checksum of a data stream
5
Copyright (C) 1995-1998 Mark Adler
8
Copyright (C) 1998 by Jacques Nomssi Nzali
9
For conditions of distribution and use, see copyright notice in readme.txt
16
function adler32(adler : cardinal; buf : Pbyte; len : cardinal) : cardinal;
18
{ Update a running Adler-32 checksum with the bytes buf[0..len-1] and
19
return the updated checksum. If buf is NIL, this function returns
20
the required initial value for the checksum.
21
An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
22
much faster. Usage example:
27
adler := adler32(0, nil, 0);
29
while (read_buffer(buffer, length) <> EOF) do
30
adler := adler32(adler, buffer, length);
32
if (adler <> original_adler) then
40
BASE = cardinal(65521); { largest prime smaller than 65536 }
41
{NMAX = 5552; original code with unsigned 32 bit integer }
42
{ NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 }
43
NMAX = 3854; { code with signed 32 bit integer }
44
{ NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^31-1 }
45
{ The penalty is the time loss in the extra MOD-calls. }
48
{ ========================================================================= }
50
function adler32(adler : cardinal; buf : Pbyte; len : cardinal) : cardinal;
55
s1 := adler and $ffff;
56
s2 := (adler shr 16) and $ffff;
58
if not Assigned(buf) then
60
adler32 := cardinal(1);
96
adler32 := (s2 shl 16) or s1;
105
#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
106
#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
107
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
108
#define DO16(buf) DO8(buf,0); DO8(buf,8);