3 {
4 adler32.c -- compute the Adler-32 checksum of a data stream
5 Copyright (C) 1995-1998 Mark Adler
7 Pascal tranlastion
8 Copyright (C) 1998 by Jacques Nomssi Nzali
9 For conditions of distribution and use, see copyright notice in readme.txt
10 }
12 interface
14 {$I imzconf.inc}
16 uses
17 imzutil;
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:
27 var
28 adler : uLong;
29 begin
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
36 error();
37 end;
38 }
40 implementation
42 const
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 }
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 { ========================================================================= }
54 var
57 begin
62 begin
64 exit;
68 begin
70 k := len
71 else
74 {
75 while (k >= 16) do
76 begin
77 DO16(buf);
78 Inc(buf, 16);
79 Dec(k, 16);
80 end;
81 if (k <> 0) then
82 repeat
83 Inc(s1, buf^);
84 Inc(puf);
85 Inc(s2, s1);
86 Dec(k);
87 until (k = 0);
88 }
90 begin
102 {
103 #define DO1(buf,i)
104 begin
105 Inc(s1, buf[i]);
106 Inc(s2, s1);
107 end;
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);
112 }