function CRCSetup(
var CRCDef: TCRCDef; Polynomial, Bits, InitVector,
FinalVector: Cardinal; Inverse: LongBool): Boolean;
// initialize CRCDef according to the parameters, calculate the lookup table
var
Value, XorValue, OldValue: Cardinal;
Index: Integer;
B: Boolean;
One: Byte;
begin
if Bits >= 8
then
begin
CRCDef.Polynomial := Polynomial;
CRCDef.Bits := Bits;
CRCDef.CRC := InitVector;
CRCDef.InitVector := InitVector;
CRCDef.FinalVector := FinalVector;
CRCDef.Inverse := Inverse;
CRCDef.Shift := Bits - 8;
Bits := -(Bits - 32);
CRCDef.Mask := -1
shr Byte(Bits);
if Inverse
then
begin
Bits := CRCDef.Bits;
XorValue := 0;
repeat
Inc(XorValue, XorValue + Ord(Polynomial
and $1));
Polynomial := Polynomial
shr 1;
Dec(Bits);
until Bits = 0;
One := $1;
for Index := 255
downto 0
do
begin
Value :=
Index;
B := Boolean(Value
and One); Value := Value
shr 1;
if B
then Value := Value
xor XorValue;
B := Boolean(Value
and One); Value := Value
shr 1;
if B
then Value := Value
xor XorValue;
B := Boolean(Value
and One); Value := Value
shr 1;
if B
then Value := Value
xor XorValue;
B := Boolean(Value
and One); Value := Value
shr 1;
if B
then Value := Value
xor XorValue;
B := Boolean(Value
and One); Value := Value
shr 1;
if B
then Value := Value
xor XorValue;
B := Boolean(Value
and One); Value := Value
shr 1;
if B
then Value := Value
xor XorValue;
B := Boolean(Value
and One); Value := Value
shr 1;
if B
then Value := Value
xor XorValue;
B := Boolean(Value
and One); Value := Value
shr 1;
if B
then Value := Value
xor XorValue;
CRCDef.Table[
Index] := Value;
end;
end
else
begin
XorValue := Polynomial
and CRCDef.Mask;
XorValue := (XorValue
shl Byte(Bits))
or (XorValue
shr (32 - Byte(Bits)));
for Index := 255
downto 0
do
begin
B := Boolean(
Index and $000000080); Value :=
Index shl 25;
if B
then Value := Value
xor XorValue;
OldValue := Value; Inc(Value, Value);
if Value < OldValue
then Value := Value
xor XorValue;
OldValue := Value; Inc(Value, Value);
if Value < OldValue
then Value := Value
xor XorValue;
OldValue := Value; Inc(Value, Value);
if Value < OldValue
then Value := Value
xor XorValue;
OldValue := Value; Inc(Value, Value);
if Value < OldValue
then Value := Value
xor XorValue;
OldValue := Value; Inc(Value, Value);
if Value < OldValue
then Value := Value
xor XorValue;
OldValue := Value; Inc(Value, Value);
if Value < OldValue
then Value := Value
xor XorValue;
OldValue := Value; Inc(Value, Value);
if Value < OldValue
then Value := Value
xor XorValue;
Value := (Value
shr Byte(Bits))
or (Value
shl (32 - Byte(Bits)));
CRCDef.Table[
Index] := Value;
end;
end;
Result := True;
end
else
Result := False;
end;