Browse Source

refactor some crc methods in hashlib

Ugochukwu Mmaduekwe 6 years ago
parent
commit
57cecb1151
2 changed files with 75 additions and 66 deletions
  1. 67 58
      src/libraries/hashlib4pascal/HlpCRC.pas
  2. 8 8
      src/libraries/hashlib4pascal/HlpICRC.pas

+ 67 - 58
src/libraries/hashlib4pascal/HlpCRC.pas

@@ -25,6 +25,7 @@ uses
 
 
 resourcestring
 resourcestring
   SUnSupportedCRCType = 'UnSupported CRC Type: "%s"';
   SUnSupportedCRCType = 'UnSupported CRC Type: "%s"';
+  SWidthOutOfRange = 'Width Must be Between 3 and 64. "%d"';
 
 
 {$REGION 'CRC Standards'}
 {$REGION 'CRC Standards'}
 
 
@@ -548,9 +549,9 @@ type
 
 
     FNames: THashLibStringArray;
     FNames: THashLibStringArray;
     FWidth: Int32;
     FWidth: Int32;
-    FPolynomial, FInit, FXorOut, FCheckValue, Fm_CRCMask, Fm_CRCHighBitMask,
-      Fm_hash: UInt64;
-    FReflectIn, FReflectOut, FIsTableGenerated: Boolean;
+    FPolynomial, FInitial, FOutputXor, FCheckValue, Fm_CRCMask,
+      Fm_CRCHighBitMask, Fm_hash: UInt64;
+    FIsInputReflected, FIsOutputReflected, FIsTableGenerated: Boolean;
 
 
     Fm_CRCTable: THashLibUInt64Array;
     Fm_CRCTable: THashLibUInt64Array;
 
 
@@ -563,19 +564,19 @@ type
     procedure SetWidth(value: Int32); inline;
     procedure SetWidth(value: Int32); inline;
     function GetPolynomial: UInt64; inline;
     function GetPolynomial: UInt64; inline;
     procedure SetPolynomial(value: UInt64); inline;
     procedure SetPolynomial(value: UInt64); inline;
-    function GetInit: UInt64; inline;
-    procedure SetInit(value: UInt64); inline;
-    function GetReflectIn: Boolean; inline;
-    procedure SetReflectIn(value: Boolean); inline;
-    function GetReflectOut: Boolean; inline;
-    procedure SetReflectOut(value: Boolean); inline;
-    function GetXOROut: UInt64; inline;
-    procedure SetXOROut(value: UInt64); inline;
+    function GetInitial: UInt64; inline;
+    procedure SetInitial(value: UInt64); inline;
+    function GetIsInputReflected: Boolean; inline;
+    procedure SetIsInputReflected(value: Boolean); inline;
+    function GetIsOutputReflected: Boolean; inline;
+    procedure SetIsOutputReflected(value: Boolean); inline;
+    function GetOutputXor: UInt64; inline;
+    procedure SetOutputXor(value: UInt64); inline;
     function GetCheckValue: UInt64; inline;
     function GetCheckValue: UInt64; inline;
     procedure SetCheckValue(value: UInt64); inline;
     procedure SetCheckValue(value: UInt64); inline;
 
 
     procedure GenerateTable();
     procedure GenerateTable();
-    // tables work only for 8, 16, 24, 32 bit CRC
+    // tables work only for CRCs with width > 7
     procedure CalculateCRCbyTable(a_data: PByte; a_data_length, a_index: Int32);
     procedure CalculateCRCbyTable(a_data: PByte; a_data_length, a_index: Int32);
     // fast bit by bit algorithm without augmented zero bytes.
     // fast bit by bit algorithm without augmented zero bytes.
     // does not use lookup table, suited for polynomial orders between 1...32.
     // does not use lookup table, suited for polynomial orders between 1...32.
@@ -587,10 +588,12 @@ type
     property Names: THashLibStringArray read GetNames write SetNames;
     property Names: THashLibStringArray read GetNames write SetNames;
     property Width: Int32 read GetWidth write SetWidth;
     property Width: Int32 read GetWidth write SetWidth;
     property Polynomial: UInt64 read GetPolynomial write SetPolynomial;
     property Polynomial: UInt64 read GetPolynomial write SetPolynomial;
-    property Init: UInt64 read GetInit write SetInit;
-    property ReflectIn: Boolean read GetReflectIn write SetReflectIn;
-    property ReflectOut: Boolean read GetReflectOut write SetReflectOut;
-    property XOROut: UInt64 read GetXOROut write SetXOROut;
+    property Initial: UInt64 read GetInitial write SetInitial;
+    property IsInputReflected: Boolean read GetIsInputReflected
+      write SetIsInputReflected;
+    property IsOutputReflected: Boolean read GetIsOutputReflected
+      write SetIsOutputReflected;
+    property OutputXor: UInt64 read GetOutputXor write SetOutputXor;
     property CheckValue: UInt64 read GetCheckValue write SetCheckValue;
     property CheckValue: UInt64 read GetCheckValue write SetCheckValue;
 
 
   strict protected
   strict protected
@@ -598,9 +601,9 @@ type
 
 
   public
   public
 
 
-    constructor Create(_Width: Int32; _poly, _Init: UInt64;
-      _refIn, _refOut: Boolean; _XorOut, _check: UInt64;
-      const _Names: THashLibStringArray);
+    constructor Create(AWidth: Int32; APolynomial, AInitial: UInt64;
+      AIsInputReflected, AIsOutputReflected: Boolean;
+      AOutputXor, ACheckValue: UInt64; const ANames: THashLibStringArray);
 
 
     procedure Initialize(); override;
     procedure Initialize(); override;
     procedure TransformBytes(const a_data: THashLibByteArray;
     procedure TransformBytes(const a_data: THashLibByteArray;
@@ -622,9 +625,9 @@ begin
   result := FCheckValue;
   result := FCheckValue;
 end;
 end;
 
 
-function TCRC.GetInit: UInt64;
+function TCRC.GetInitial: UInt64;
 begin
 begin
-  result := FInit;
+  result := FInitial;
 end;
 end;
 
 
 function TCRC.GetNames: THashLibStringArray;
 function TCRC.GetNames: THashLibStringArray;
@@ -637,14 +640,14 @@ begin
   result := FPolynomial;
   result := FPolynomial;
 end;
 end;
 
 
-function TCRC.GetReflectIn: Boolean;
+function TCRC.GetIsInputReflected: Boolean;
 begin
 begin
-  result := FReflectIn;
+  result := FIsInputReflected;
 end;
 end;
 
 
-function TCRC.GetReflectOut: Boolean;
+function TCRC.GetIsOutputReflected: Boolean;
 begin
 begin
-  result := FReflectOut;
+  result := FIsOutputReflected;
 end;
 end;
 
 
 function TCRC.GetWidth: Int32;
 function TCRC.GetWidth: Int32;
@@ -652,9 +655,9 @@ begin
   result := FWidth;
   result := FWidth;
 end;
 end;
 
 
-function TCRC.GetXOROut: UInt64;
+function TCRC.GetOutputXor: UInt64;
 begin
 begin
-  result := FXorOut;
+  result := FOutputXor;
 end;
 end;
 
 
 procedure TCRC.SetCheckValue(value: UInt64);
 procedure TCRC.SetCheckValue(value: UInt64);
@@ -662,9 +665,9 @@ begin
   FCheckValue := value;
   FCheckValue := value;
 end;
 end;
 
 
-procedure TCRC.SetInit(value: UInt64);
+procedure TCRC.SetInitial(value: UInt64);
 begin
 begin
-  FInit := value;
+  FInitial := value;
 end;
 end;
 
 
 procedure TCRC.SetNames(const value: THashLibStringArray);
 procedure TCRC.SetNames(const value: THashLibStringArray);
@@ -677,14 +680,14 @@ begin
   FPolynomial := value;
   FPolynomial := value;
 end;
 end;
 
 
-procedure TCRC.SetReflectIn(value: Boolean);
+procedure TCRC.SetIsInputReflected(value: Boolean);
 begin
 begin
-  FReflectIn := value;
+  FIsInputReflected := value;
 end;
 end;
 
 
-procedure TCRC.SetReflectOut(value: Boolean);
+procedure TCRC.SetIsOutputReflected(value: Boolean);
 begin
 begin
-  FReflectOut := value;
+  FIsOutputReflected := value;
 end;
 end;
 
 
 procedure TCRC.SetWidth(value: Int32);
 procedure TCRC.SetWidth(value: Int32);
@@ -692,9 +695,9 @@ begin
   FWidth := value;
   FWidth := value;
 end;
 end;
 
 
-procedure TCRC.SetXOROut(value: UInt64);
+procedure TCRC.SetOutputXor(value: UInt64);
 begin
 begin
-  FXorOut := value;
+  FOutputXor := value;
 end;
 end;
 
 
 function TCRC.GetName: String;
 function TCRC.GetName: String;
@@ -713,7 +716,7 @@ begin
   i := a_index;
   i := a_index;
   tmp := Fm_hash;
   tmp := Fm_hash;
 
 
-  if (ReflectIn) then
+  if (IsInputReflected) then
   begin
   begin
     while Length > 0 do
     while Length > 0 do
     begin
     begin
@@ -751,7 +754,7 @@ begin
   while Length > 0 do
   while Length > 0 do
   begin
   begin
     c := UInt64(a_data[i]);
     c := UInt64(a_data[i]);
-    if (ReflectIn) then
+    if (IsInputReflected) then
     begin
     begin
       c := Reflect(c, 8);
       c := Reflect(c, 8);
     end;
     end;
@@ -777,8 +780,8 @@ function TCRC.Clone(): IHash;
 var
 var
   HashInstance: TCRC;
   HashInstance: TCRC;
 begin
 begin
-  HashInstance := TCRC.Create(Width, Polynomial, Init, ReflectIn, ReflectOut,
-    XOROut, CheckValue, System.Copy(Names));
+  HashInstance := TCRC.Create(Width, Polynomial, Initial, IsInputReflected,
+    IsOutputReflected, OutputXor, CheckValue, System.Copy(Names));
   HashInstance.Fm_CRCMask := Fm_CRCMask;
   HashInstance.Fm_CRCMask := Fm_CRCMask;
   HashInstance.Fm_CRCHighBitMask := Fm_CRCHighBitMask;
   HashInstance.Fm_CRCHighBitMask := Fm_CRCHighBitMask;
   HashInstance.Fm_hash := Fm_hash;
   HashInstance.Fm_hash := Fm_hash;
@@ -788,16 +791,22 @@ begin
   result.BufferSize := BufferSize;
   result.BufferSize := BufferSize;
 end;
 end;
 
 
-constructor TCRC.Create(_Width: Int32; _poly, _Init: UInt64;
-  _refIn, _refOut: Boolean; _XorOut, _check: UInt64;
-  const _Names: THashLibStringArray);
+constructor TCRC.Create(AWidth: Int32; APolynomial, AInitial: UInt64;
+  AIsInputReflected, AIsOutputReflected: Boolean;
+  AOutputXor, ACheckValue: UInt64; const ANames: THashLibStringArray);
 begin
 begin
 
 
+  if not(AWidth in [3 .. 64]) then
+  begin
+    raise EArgumentOutOfRangeHashLibException.CreateResFmt(@SWidthOutOfRange,
+      [AWidth]);
+  end;
+
   FIsTableGenerated := False;
   FIsTableGenerated := False;
 
 
   Inherited Create(-1, -1); // Dummy State
   Inherited Create(-1, -1); // Dummy State
 
 
-  case _Width of
+  case AWidth of
     0 .. 7:
     0 .. 7:
       begin
       begin
         Self.HashSize := 1;
         Self.HashSize := 1;
@@ -824,14 +833,14 @@ begin
 
 
   end;
   end;
 
 
-  Names := _Names;
-  Width := _Width;
-  Polynomial := _poly;
-  Init := _Init;
-  ReflectIn := _refIn;
-  ReflectOut := _refOut;
-  XOROut := _XorOut;
-  CheckValue := _check;
+  Names := ANames;
+  Width := AWidth;
+  Polynomial := APolynomial;
+  Initial := AInitial;
+  IsInputReflected := AIsInputReflected;
+  IsOutputReflected := AIsOutputReflected;
+  OutputXor := AOutputXor;
+  CheckValue := ACheckValue;
 
 
 end;
 end;
 
 
@@ -1275,7 +1284,7 @@ begin
   while i < 256 do
   while i < 256 do
   begin
   begin
     crc := UInt64(i);
     crc := UInt64(i);
-    if (ReflectIn) then
+    if (IsInputReflected) then
     begin
     begin
       crc := Reflect(crc, 8);
       crc := Reflect(crc, 8);
     end;
     end;
@@ -1291,7 +1300,7 @@ begin
       System.Inc(j);
       System.Inc(j);
     end;
     end;
 
 
-    if (ReflectIn) then
+    if (IsInputReflected) then
     begin
     begin
       crc := Reflect(crc, Width);
       crc := Reflect(crc, Width);
     end;
     end;
@@ -1308,7 +1317,7 @@ begin
   // initialize some bitmasks
   // initialize some bitmasks
   Fm_CRCMask := (((UInt64(1) shl (Width - 1)) - 1) shl 1) or 1;
   Fm_CRCMask := (((UInt64(1) shl (Width - 1)) - 1) shl 1) or 1;
   Fm_CRCHighBitMask := UInt64(1) shl (Width - 1);
   Fm_CRCHighBitMask := UInt64(1) shl (Width - 1);
-  Fm_hash := Init;
+  Fm_hash := Initial;
 
 
   if (Width > Delta) then // then use table
   if (Width > Delta) then // then use table
   begin
   begin
@@ -1318,7 +1327,7 @@ begin
       GenerateTable();
       GenerateTable();
     end;
     end;
 
 
-    if (ReflectIn) then
+    if (IsInputReflected) then
       Fm_hash := Reflect(Fm_hash, Width);
       Fm_hash := Reflect(Fm_hash, Width);
 
 
   end;
   end;
@@ -1386,20 +1395,20 @@ begin
 
 
   if Width > Delta then
   if Width > Delta then
   begin
   begin
-    if (ReflectIn xor ReflectOut) then
+    if (IsInputReflected xor IsOutputReflected) then
     begin
     begin
       Fm_hash := Reflect(Fm_hash, Width);
       Fm_hash := Reflect(Fm_hash, Width);
     end;
     end;
   end
   end
   else
   else
   begin
   begin
-    if (ReflectOut) then
+    if (IsOutputReflected) then
     begin
     begin
       Fm_hash := Reflect(Fm_hash, Width);
       Fm_hash := Reflect(Fm_hash, Width);
     end;
     end;
   end;
   end;
 
 
-  Fm_hash := Fm_hash xor XOROut;
+  Fm_hash := Fm_hash xor OutputXor;
   Fm_hash := Fm_hash and Fm_CRCMask;
   Fm_hash := Fm_hash and Fm_CRCMask;
 
 
   if Width = 21 then // special case
   if Width = 21 then // special case

+ 8 - 8
src/libraries/hashlib4pascal/HlpICRC.pas

@@ -19,14 +19,14 @@ type
     property Width: Int32 read GetWidth;
     property Width: Int32 read GetWidth;
     function GetPolynomial: UInt64;
     function GetPolynomial: UInt64;
     property Polynomial: UInt64 read GetPolynomial;
     property Polynomial: UInt64 read GetPolynomial;
-    function GetInit: UInt64;
-    property Init: UInt64 read GetInit;
-    function GetReflectIn: Boolean;
-    property ReflectIn: Boolean read GetReflectIn;
-    function GetReflectOut: Boolean;
-    property ReflectOut: Boolean read GetReflectOut;
-    function GetXOROut: UInt64;
-    property XOROut: UInt64 read GetXOROut;
+    function GetInitial: UInt64;
+    property Initial: UInt64 read GetInitial;
+    function GetIsInputReflected: Boolean;
+    property IsInputReflected: Boolean read GetIsInputReflected;
+    function GetIsOutputReflected: Boolean;
+    property IsOutputReflected: Boolean read GetIsOutputReflected;
+    function GetOutputXor: UInt64;
+    property OutputXor: UInt64 read GetOutputXor;
     function GetCheckValue: UInt64;
     function GetCheckValue: UInt64;
     property CheckValue: UInt64 read GetCheckValue;
     property CheckValue: UInt64 read GetCheckValue;