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
19 function adler32(adler
: uLong
; buf
: pBytef
; len
: uInt
) : uLong
;
21 { Update a running Adler-32 checksum with the bytes buf[0..len-1] and
22 return the updated checksum. If buf is NIL, this function returns
23 the required initial value for the checksum.
24 An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
25 much faster. Usage example:
30 adler := adler32(0, Z_NULL, 0);
32 while (read_buffer(buffer, length) <> EOF) do
33 adler := adler32(adler, buffer, length);
35 if (adler <> original_adler) then
43 BASE
= uLong(65521); { largest prime smaller than 65536 }
44 {NMAX = 5552; original code with unsigned 32 bit integer }
45 { NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 }
46 NMAX
= 3854; { code with signed 32 bit integer }
47 { NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^31-1 }
48 { The penalty is the time loss in the extra MOD-calls. }
51 { ========================================================================= }
53 function adler32(adler
: uLong
; buf
: pBytef
; len
: uInt
) : uLong
;
58 s1
:= adler
and $ffff;
59 s2
:= (adler
shr 16) and $ffff;
61 if not Assigned(buf
) then
99 adler32
:= (s2
shl 16) or s1
;
108 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
109 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
110 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
111 #define DO16(buf) DO8(buf,0); DO8(buf,8);