-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbcrc64.pas
399 lines (313 loc) · 10.8 KB
/
bcrc64.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
unit bCRC64;
{64 Bit CRC, bitwise without table}
interface
(*************************************************************************
DESCRIPTION : 64 Bit CRC, bitwise without table
REQUIREMENTS : TP5-7, D1-D7/D9-D10/D12/D17, FPC, VP
EXTERNAL DATA : ---
MEMORY USAGE : ---
DISPLAY MODE : ---
REFERENCES : http://www.ecma-international.org/publications/files/ecma-st/
Ecma-182.pdf: DLT1 spec or Ecma-231.pdf: DLT4 spec
Version Date Author Modification
------- -------- ------- ------------------------------------------
1.00 07.07.03 W.Ehrhardt Initial version based on bCRC32 layout
1.10 30.08.03 we Common vers., XL versions for Win32
2.10 31.08.03 we Common vers., new polynomial
2.20 27.09.03 we FPC/go32v2
2.30 05.10.03 we STD.INC, TP5.0
2.40 10.10.03 we common version, english comments
3.00 01.12.03 we Common version 3.0
3.01 17.12.05 we Force $I- in bCRC64File
3.02 07.08.06 we $ifdef BIT32: (const fname: shortstring...)
3.03 10.02.07 we bCRC64File: no eof, XL and filemode via $ifdef
3.04 04.10.07 we FPC: {$asmmode intel}
3.05 12.11.08 we uses BTypes, Ptr2Inc and/or Str255
3.06 19.07.09 we D12 fix: assign with typecast string(fname)
3.07 08.03.12 we {$ifndef BIT16} instead of {$ifdef WIN32}
3.08 26.12.12 we D17 and PurePascal
**************************************************************************)
(*-------------------------------------------------------------------------
(C) Copyright 2002-2012 Wolfgang Ehrhardt
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in
a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
----------------------------------------------------------------------------*)
{$i STD.INC}
{$ifdef BIT64}
{$ifndef PurePascal}
{$define PurePascal}
{$endif}
{$endif}
uses
BTypes;
type
TCRC64b = packed record
lo32, hi32: longint;
end;
procedure bCRC64Init(var CRC: TCRC64b);
{-CRC64 initialization}
procedure bCRC64Update(var CRC: TCRC64b; Msg: pointer; Len: word);
{-update CRC64 with Msg data}
procedure bCRC64Final(var CRC: TCRC64b);
{-CRC64: finalize calculation}
function bCRC64SelfTest: boolean;
{-Self test for CRC64}
procedure bCRC64Full(var CRC: TCRC64b; Msg: pointer; Len: word);
{-CRC64 of Msg with init/update/final}
procedure bCRC64File({$ifdef CONST} const {$endif} fname: Str255;
var CRC: TCRC64b; var buf; bsize: word; var Err: word);
{-CRC64 of file, buf: buffer with at least bsize bytes}
{$ifndef BIT16}
procedure bCRC64UpdateXL(var CRC: TCRC64b; Msg: pointer; Len: longint);
{-update CRC64 with Msg data}
procedure bCRC64FullXL(var CRC: TCRC64b; Msg: pointer; Len: longint);
{-CRC64 of Msg with init/update/final}
{$endif}
implementation
{$ifdef FPC}
{$asmmode intel}
{$endif}
(*************************************************************************
T_CTab64 - CRC64 table calculation (c) 2002-2004 W.Ehrhardt
Calculate CRC64 tables for polynomial:
x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 +
x^40 + x^39 + x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 +
x^24 + x^23 + x^22 + x^21 + x^19 + x^17 + x^13 + x^12 + x^10 + x^9 +
x^7 + x^4 + x^1 + 1
const
PolyLo : longint = longint($A9EA3693);
PolyHi : longint = longint($42F0E1EB);
*************************************************************************)
const
Mask64: TCRC64b = (lo32:-1; hi32:-1);
Poly : TCRC64b = (lo32:longint($A9EA3693); hi32:longint($42F0E1EB));
{
for i:=1 to length(test) do begin
c64 := c64 xor test[i] shl 56;
for b := 1 to 8 do begin
if c64<0 then c64 := (c64 shl 1) xor Poly else c64 := c64 shl 1;
end;
end;
}
{$ifndef BIT16}
{$ifdef PurePascal}
{---------------------------------------------------------------------------}
procedure bCRC64UpdateXL(var CRC: TCRC64b; Msg: pointer; Len: longint);
{-update CRC64 with Msg data}
var
i,b: longint;
c64: int64 absolute CRC;
var
Poly64: int64 absolute Poly; {FPC1 does not like int64 typed consts}
begin
for i:=1 to Len do begin
c64 := c64 xor (int64(PByte(Msg)^) shl 56);
for b := 1 to 8 do begin
if c64<0 then c64 := (c64 shl 1) xor Poly64 else c64 := c64 shl 1;
end;
inc(Ptr2Inc(Msg));
end;
end;
{$else}
{---------------------------------------------------------------------------}
procedure bCRC64UpdateXL(var CRC: TCRC64b; Msg: pointer; Len: longint);
{-update CRC64 with Msg data}
begin
asm
push ebx
push esi
mov ecx,[Len]
jecxz @@4
mov eax,[CRC]
mov edx,[eax+4]
mov eax,[eax]
mov esi,[Msg]
@@1: mov bl,byte ptr [esi]
inc esi
shl ebx,24
xor edx,ebx
mov ebx,8
@@2: shl eax,1
rcl edx,1
jnc @@3
xor eax,Poly.lo32
xor edx,Poly.hi32
@@3: dec ebx
jnz @@2
dec ecx
jnz @@1
mov ebx,[CRC]
mov [ebx],eax
mov [ebx+4],edx
@@4: pop esi
pop ebx
end;
end;
{$endif}
{---------------------------------------------------------------------------}
procedure bCRC64Update(var CRC: TCRC64b; Msg: pointer; Len: word);
{-update CRC64 with Msg data}
begin
bCRC64UpdateXL(CRC, Msg, Len);
end;
{$else}
(**** TP5-7/Delphi1 for 386+ *****)
{$ifndef BASM16}
{---------------------------------------------------------------------------}
procedure bCRC64Update(var CRC: TCRC64b; Msg: pointer; Len: word);
{-update CRC64 with Msg data}
var
i,b: word;
xp: boolean;
begin
for i:=1 to Len do begin
CRC.hi32 := CRC.hi32 xor (longint(pByte(Msg)^) shl 24);
for b := 1 to 8 do begin
xp := CRC.hi32<0;
CRC.hi32 := CRC.hi32 shl 1;
if CRC.lo32<0 then inc(CRC.hi32);
CRC.lo32 := CRC.lo32 shl 1;
if xp then begin
CRC.lo32 := CRC.lo32 xor Poly.lo32;
CRC.hi32 := CRC.hi32 xor Poly.hi32;
end;
end;
inc(Ptr2Inc(Msg));
end;
end;
{$else}
{TP 6/7/Delphi1}
{---------------------------------------------------------------------------}
procedure bCRC64Update(var CRC: TCRC64b; Msg: pointer; Len: word); assembler;
{-update CRC64 with Msg data}
asm
les si,[CRC]
db $66; mov ax,es:[si]
db $66; mov dx,es:[si+4]
les si,[Msg]
mov cx,[len]
or cx,cx
jz @@4
@@1: mov bl,es:[si] {loop for Len bytes}
inc si
db $66; shl bx,24
db $66; xor dx,bx
mov bx,8
@@2: db $66; shl ax,1 {loop for 8 bit}
db $66; rcl dx,1
jnc @@3
db $66; xor ax,word ptr Poly.lo32
db $66; xor dx,word ptr Poly.hi32
@@3: dec bx
jnz @@2
dec cx
jnz @@1
les si,CRC {store result}
db $66; mov es:[si],ax
db $66; mov es:[si+4],dx
@@4:
end;
{$endif BASM16}
{$endif BIT16}
{$ifndef BIT16}
{---------------------------------------------------------------------------}
procedure bCRC64FullXL(var CRC: TCRC64b; Msg: pointer; Len: longint);
{-CRC64 of Msg with init/update/final}
begin
bCRC64Init(CRC);
bCRC64UpdateXL(CRC, Msg, Len);
bCRC64Final(CRC);
end;
{$endif}
{---------------------------------------------------------------------------}
procedure bCRC64Init(var CRC: TCRC64b);
{-CRC64 initialization}
begin
CRC := Mask64;
end;
{---------------------------------------------------------------------------}
procedure bCRC64Final(var CRC: TCRC64b);
{-CRC64: finalize calculation}
begin
CRC.lo32 := CRC.lo32 xor Mask64.lo32;
CRC.hi32 := CRC.hi32 xor Mask64.hi32;
end;
{---------------------------------------------------------------------------}
function bCRC64SelfTest: boolean;
{-self test for CRC64}
const
s: string[20] = '123456789';
CHKhi: longint = longint($62EC59E3);
CHKlo: longint = longint($F1A4F00A);
var
CRC: TCRC64b;
begin
bCRC64Init(CRC);
bCRC64Update(CRC, @s[1], length(s));
bCRC64Final(CRC);
bCRC64SelfTest := (CRC.lo32=CHKlo) and (CRC.hi32=CHKhi);
end;
{---------------------------------------------------------------------------}
procedure bCRC64Full(var CRC: TCRC64b; Msg: pointer; Len: word);
{-CRC64 of Msg with init/update/final}
begin
bCRC64Init(CRC);
bCRC64Update(CRC, Msg, Len);
bCRC64Final(CRC);
end;
{$i-} {Force I-}
{---------------------------------------------------------------------------}
procedure bCRC64File({$ifdef CONST} const {$endif} fname: Str255;
var CRC: TCRC64b; var buf; bsize: word; var Err: word);
{-CRC64 of file, buf: buffer with at least bsize bytes}
var
{$ifdef VirtualPascal}
fms: word;
{$else}
fms: byte;
{$endif}
{$ifndef BIT16}
L: longint;
{$else}
L: word;
{$endif}
f: file;
begin
fms := FileMode;
{$ifdef VirtualPascal}
FileMode := $40; {open_access_ReadOnly or open_share_DenyNone;}
{$else}
FileMode := 0;
{$endif}
system.assign(f,{$ifdef D12Plus} string {$endif} (fname));
system.reset(f,1);
Err := IOResult;
FileMode := fms;
if Err<>0 then exit;
bCRC64Init(CRC);
L := bsize;
while (Err=0) and (L=bsize) do begin
system.blockread(f,buf,bsize,L);
Err := IOResult;
{$ifndef BIT16}
bCRC64UpdateXL(CRC, @buf, L);
{$else}
bCRC64Update(CRC, @buf, L);
{$endif}
end;
system.close(f);
if IOResult=0 then;
bCRC64Final(CRC);
end;
end.