DEADSOFTWARE

hopefully no more windows
[d2df-editor.git] / src / lib / vampimg / ZLib / imadler.pas
1 Unit imadler;
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;
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:
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
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;
54 var
55 s1, s2 : uLong;
56 k : int;
57 begin
58 s1 := adler and $ffff;
59 s2 := (adler shr 16) and $ffff;
61 if not Assigned(buf) then
62 begin
63 adler32 := uLong(1);
64 exit;
65 end;
67 while (len > 0) do
68 begin
69 if len < NMAX then
70 k := len
71 else
72 k := NMAX;
73 Dec(len, k);
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 }
89 while (k > 0) do
90 begin
91 Inc(s1, buf^);
92 Inc(s2, s1);
93 Inc(buf);
94 Dec(k);
95 end;
96 s1 := s1 mod BASE;
97 s2 := s2 mod BASE;
98 end;
99 adler32 := (s2 shl 16) or s1;
100 end;
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);
113 end.