-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRC_Unit.pas
More file actions
70 lines (56 loc) · 2.06 KB
/
Copy pathCRC_Unit.pas
File metadata and controls
70 lines (56 loc) · 2.06 KB
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
{ *********************************************************************
Code written by Truong Hy.
Provides 2 functions to calculate:
- CRC16 X25 standard
- CRC32
values.
It uses a table (array) of already calculated XORing values for
optimised (byte wise XOR) calculation of CRC value.
The code can still be further optimised (even faster calculation if
CPU supports this) by using word/long word wise XORing. New tables
will have to be used. Although this will have it's limitations:
- CPU should have word/long word XOR which is faster than byte wise
XOR
- the input must be word/long word aligned
********************************************************************* }
unit CRC_Unit;
interface
{ ***********************************************************************
Include table (array) of already calculated XORing values for optimised
(byte wise) calculation of CRC value.
*********************************************************************** }
{$I CRC16_table.PAS}
{$I CRC32_table.PAS}
Type p_byte=^Byte;
{ Calculates CRC16 X25 standard value with output and input bits not reversed (normal).
Uses forward CRC algorithm. }
Function Calc_CRC16(p_buf : Pointer; len : LongWord) : Word;
{ Calculates CRC32 value with output and input bits reversed.
Uses reversed CRC algorithm. }
Function Calc_CRC32(p_buf : Pointer; len : LongWord) : LongWord;
implementation
Function Calc_CRC16(p_buf : Pointer; len : LongWord) : Word;
Var i : LongWord;
crc_val : Word;
Begin
crc_val:=0;
For i:=1 To len Do
Begin
crc_val:=CRC16_tab[((crc_val SHR 8) XOR p_byte(p_buf)^) AND $00FF] XOR (crc_val SHL 8);
Inc(p_byte(p_buf));
End;
Calc_CRC16:=crc_val;
End;
Function Calc_CRC32(p_buf : Pointer; len : LongWord) : LongWord;
Var i : LongWord;
crc_val : LongWord;
Begin
crc_val:=0;
For i:=1 To len Do
Begin
crc_val:=CRC32_tab[(crc_val XOR p_byte(p_buf)^) AND $000000FF] XOR (crc_val SHR 8);
Inc(p_byte(p_buf));
End;
Calc_CRC32:=crc_val;
End;
end.