forked from thargor6/mb3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdler32.pas
64 lines (54 loc) · 1.16 KB
/
Adler32.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
////////////////////////////////////////////////////////////////////////////////
//
// Adler32.pas - Adler32 data integrity check
// ------------------------------------------
// Version: 2003-05-15
// Maintain: Michael Vinther | mv@logicnet·dk
//
// Last changes:
//
unit Adler32;
interface
resourcestring
rsAdler32Error = 'Adler32 error';
type
TAdler32 = object
public
procedure Init;
procedure Calc(var Data; Length: Cardinal);
procedure Update(Data: Byte);
function CheckValue: Cardinal;
private
s1, s2 : Cardinal;
end;
implementation
uses SysUtils, MemUtils;
procedure TAdler32.Init;
begin
s1:=1;
s2:=0;
end;
procedure TAdler32.Calc(var Data; Length: Cardinal);
var
I : Integer;
begin
for I:=0 to Length-1 do
begin
Inc(s1,TByteArray(Data)[I]);
if s1>=65521 then Dec(s1,65521);
Inc(s2,s1);
if s2>=65521 then Dec(s2,65521);
end;
end;
procedure TAdler32.Update(Data: Byte);
begin
Inc(s1,Data);
if s1>=65521 then Dec(s1,65521);
Inc(s2,s1);
if s2>=65521 then Dec(s2,65521);
end;
function TAdler32.CheckValue: Cardinal;
begin
Result:=GetSwap4(Cardinal(s2) shl 16 or s1);
end;
end.