author | Xeli |
Sat, 24 Mar 2012 15:52:07 +0100 | |
changeset 6812 | 929b467c7277 |
parent 6580 | 6155187bf599 |
child 6926 | 6e832f8f4d8e |
permissions | -rw-r--r-- |
3744 | 1 |
unit Adler32; |
2 |
||
3 |
{ZLib - Adler32 checksum function} |
|
4 |
||
5 |
||
6 |
interface |
|
7 |
||
8 |
(************************************************************************* |
|
9 |
||
10 |
DESCRIPTION : ZLib - Adler32 checksum function |
|
11 |
||
12 |
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12, FPC, VP |
|
13 |
||
14 |
EXTERNAL DATA : --- |
|
15 |
||
16 |
MEMORY USAGE : --- |
|
17 |
||
18 |
DISPLAY MODE : --- |
|
19 |
||
20 |
REFERENCES : RFC 1950 (http://tools.ietf.org/html/rfc1950) |
|
21 |
||
22 |
||
23 |
Version Date Author Modification |
|
24 |
------- -------- ------- ------------------------------------------ |
|
25 |
0.10 30.08.03 W.Ehrhardt Initial version based on MD5 layout |
|
26 |
2.10 30.08.03 we Common vers., XL versions for Win32 |
|
27 |
2.20 27.09.03 we FPC/go32v2 |
|
28 |
2.30 05.10.03 we STD.INC, TP5.0 |
|
29 |
2.40 10.10.03 we common version, english comments |
|
30 |
3.00 01.12.03 we Common version 3.0 |
|
31 |
3.01 22.05.05 we Adler32UpdateXL (i,n: integer) |
|
32 |
3.02 17.12.05 we Force $I- in Adler32File |
|
33 |
3.03 07.08.06 we $ifdef BIT32: (const fname: shortstring...) |
|
34 |
3.04 10.02.07 we Adler32File: no eof, XL and filemode via $ifdef |
|
35 |
3.05 04.07.07 we BASM16: speed-up factor 15 |
|
36 |
3.06 12.11.08 we uses BTypes, Ptr2Inc and/or Str255 |
|
37 |
3.07 25.04.09 we updated RFC URL(s) |
|
38 |
3.08 19.07.09 we D12 fix: assign with typecast string(fname) |
|
39 |
**************************************************************************) |
|
40 |
||
41 |
(*------------------------------------------------------------------------- |
|
42 |
(C) Copyright 2002-2009 Wolfgang Ehrhardt |
|
43 |
||
44 |
This software is provided 'as-is', without any express or implied warranty. |
|
45 |
In no event will the authors be held liable for any damages arising from |
|
46 |
the use of this software. |
|
47 |
||
48 |
Permission is granted to anyone to use this software for any purpose, |
|
49 |
including commercial applications, and to alter it and redistribute it |
|
50 |
freely, subject to the following restrictions: |
|
51 |
||
52 |
1. The origin of this software must not be misrepresented; you must not |
|
53 |
claim that you wrote the original software. If you use this software in |
|
54 |
a product, an acknowledgment in the product documentation would be |
|
55 |
appreciated but is not required. |
|
56 |
||
57 |
2. Altered source versions must be plainly marked as such, and must not be |
|
58 |
misrepresented as being the original software. |
|
59 |
||
60 |
3. This notice may not be removed or altered from any source distribution. |
|
61 |
----------------------------------------------------------------------------*) |
|
62 |
||
63 |
(* |
|
64 |
As per the license above, noting that this implementation of adler32 was stripped of everything we didn't need. |
|
65 |
That means no btypes, file loading, and the assembly version disabled. |
|
66 |
*) |
|
67 |
||
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
68 |
procedure Adler32Update ( var adler :longint; Msg :pointer; Len :longint ); |
3744 | 69 |
|
70 |
implementation |
|
71 |
||
72 |
(* |
|
73 |
$ifdef BASM16 |
|
74 |
||
75 |
procedure Adler32Update(var adler: longint; Msg: pointer; Len: longint); |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
76 |
//-update Adler32 with Msg data |
3744 | 77 |
const |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
78 |
BASE = 65521; // max. prime < 65536 |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
79 |
NMAX = 5552; // max. n with 255n(n+1)/2 + (n+1)(BASE-1) < 2^32 |
3744 | 80 |
type |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
81 |
LH = packed record |
3744 | 82 |
L,H: word; |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
83 |
end; |
3744 | 84 |
var |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
85 |
s1,s2: longint; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
86 |
n: integer; |
3744 | 87 |
begin |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
88 |
s1 := LH(adler).L; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
89 |
s2 := LH(adler).H; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
90 |
while Len > 0 do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
91 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
92 |
if Len<NMAX then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
93 |
n := Len |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
94 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
95 |
n := NMAX; |
3744 | 96 |
//BASM increases speed from about 52 cyc/byte to about 3.7 cyc/byte |
97 |
asm |
|
98 |
mov cx,[n] |
|
99 |
db $66; mov ax,word ptr [s1] |
|
100 |
db $66; mov di,word ptr [s2] |
|
101 |
les si,[msg] |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
102 |
@@1: db $66, $26, $0f, $b6, $1c // movzx ebx,es:[si] |
3744 | 103 |
inc si |
104 |
db $66; add ax,bx // inc(s1, pByte(Msg)^) |
|
105 |
db $66; add di,ax // inc(s2, s1 |
|
106 |
dec cx |
|
107 |
jnz @@1 |
|
108 |
db $66; sub cx,cx |
|
109 |
mov cx,BASE |
|
110 |
db $66; sub dx,dx |
|
111 |
db $66; div cx |
|
112 |
db $66; mov word ptr [s1],dx // s1 := s1 mod BASE |
|
113 |
db $66; sub dx,dx |
|
114 |
db $66; mov ax,di |
|
115 |
db $66; div cx |
|
116 |
db $66; mov word ptr [s2],dx // s2 := s2 mod BASE |
|
117 |
mov word ptr [msg],si // save offset for next chunk |
|
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
118 |
end; |
3744 | 119 |
dec(len, n); |
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
120 |
end; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
121 |
LH(adler).L := word(s1); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
122 |
LH(adler).H := word(s2); |
3744 | 123 |
end; |
124 |
*) |
|
125 |
||
6580
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
126 |
procedure Adler32Update(var adler: longint; Msg: pointer; Len :longint); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
127 |
{-update Adler32 with Msg data} |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
128 |
const |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
129 |
BASE = 65521; {max. prime < 65536 } |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
130 |
NMAX = 3854; {max. n with 255n(n+1)/2 + (n+1)(BASE-1) < 2^31} |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
131 |
type |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
132 |
LH = packed record |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
133 |
L, H: word; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
134 |
end; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
135 |
var |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
136 |
s1, s2: longint; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
137 |
i, n: integer; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
138 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
139 |
s1 := LH(adler).L; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
140 |
s2 := LH(adler).H; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
141 |
while Len>0 do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
142 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
143 |
if Len<NMAX then |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
144 |
n := Len |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
145 |
else |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
146 |
n := NMAX; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
147 |
|
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
148 |
for i := 1 to n do |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
149 |
begin |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
150 |
inc(s1, pByte(Msg)^); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
151 |
inc(Msg); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
152 |
inc(s2, s1); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
153 |
end; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
154 |
s1 := s1 mod BASE; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
155 |
s2 := s2 mod BASE; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
156 |
dec(len, n); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
157 |
end; |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
158 |
LH(adler).L := word(s1); |
6155187bf599
A partial reformatting of the pascal code to have consistent syntax. Things that are still inconsistent.
lovelacer
parents:
3744
diff
changeset
|
159 |
LH(adler).H := word(s2); |
3744 | 160 |
end; |
161 |
||
162 |
end. |