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