瀏覽代碼

sync hashlib4pascal library with parent repo.

Ugochukwu Mmaduekwe 6 年之前
父節點
當前提交
381cd21b1c

+ 0 - 10
src/libraries/hashlib4pascal/HashLib.inc

@@ -114,11 +114,6 @@
   // XE7 and Above
   // XE7 and Above
 {$IF CompilerVersion >= 28.0}
 {$IF CompilerVersion >= 28.0}
 {$DEFINE DELPHIXE7_UP}
 {$DEFINE DELPHIXE7_UP}
-{$IFEND}
-
-  // 10.2 Tokyo and Above
-{$IF CompilerVersion >= 32.0}
-{$DEFINE DELPHI10.2_TOKYO_UP}
 {$IFEND}
 {$IFEND}
 
 
   // 2010 and Above
   // 2010 and Above
@@ -126,11 +121,6 @@
 {$MESSAGE ERROR 'This Library requires Delphi 2010 or higher.'}
 {$MESSAGE ERROR 'This Library requires Delphi 2010 or higher.'}
 {$ENDIF}
 {$ENDIF}
 
 
-  // 10.2 Tokyo and Above
-{$IFDEF DELPHI10.2_TOKYO_UP}
-{$WARN COMBINING_SIGNED_UNSIGNED64 OFF}
-{$ENDIF}
-
 
 
 {$ENDIF DELPHI}
 {$ENDIF DELPHI}
 
 

+ 2 - 2
src/libraries/hashlib4pascal/HlpBlake2B.pas

@@ -1748,7 +1748,7 @@ begin
     begin
     begin
       System.Move(a_data[offset], F_buf[F_bufferFilled], bufferRemaining);
       System.Move(a_data[offset], F_buf[F_bufferFilled], bufferRemaining);
     end;
     end;
-    F_counter0 := F_counter0 + BlockSizeInBytes;
+    F_counter0 := F_counter0 + UInt64(BlockSizeInBytes);
     if (F_counter0 = 0) then
     if (F_counter0 = 0) then
     begin
     begin
       System.Inc(F_counter1);
       System.Inc(F_counter1);
@@ -1761,7 +1761,7 @@ begin
 
 
   while (a_data_length > BlockSizeInBytes) do
   while (a_data_length > BlockSizeInBytes) do
   begin
   begin
-    F_counter0 := F_counter0 + BlockSizeInBytes;
+    F_counter0 := F_counter0 + UInt64(BlockSizeInBytes);
     if (F_counter0 = 0) then
     if (F_counter0 = 0) then
     begin
     begin
       System.Inc(F_counter1);
       System.Inc(F_counter1);

+ 231 - 0
src/libraries/hashlib4pascal/HlpCRC32Fast.pas

@@ -0,0 +1,231 @@
+unit HlpCRC32Fast;
+
+{$I HashLib.inc}
+
+interface
+
+uses
+  HlpHashLibTypes,
+  HlpHash,
+  HlpIHash,
+  HlpIHashInfo,
+  HlpHashResult,
+  HlpIHashResult;
+
+type
+
+  TCRC32Fast = class(THash, IChecksum, IHash32, ITransformBlock)
+
+  strict protected
+  var
+    FCurrentCRC: UInt32;
+
+    procedure LocalCRCCompute(const ACRCTable: THashLibUInt32Array;
+      const AData: THashLibByteArray; AIndex, ALength: Int32);
+
+    class function Init_CRC_Table(APolynomial: UInt32)
+      : THashLibUInt32Array; static;
+
+  public
+
+    constructor Create();
+
+    procedure Initialize(); override;
+    function TransformFinal(): IHashResult; override;
+
+  end;
+
+  TCRC32_PKZIP = class sealed(TCRC32Fast)
+
+  strict private
+
+  const
+    CRC32_PKZIP_Polynomial = UInt32($EDB88320); // Polynomial Reversed
+    class var
+
+      FCRC32_PKZIP_Table: THashLibUInt32Array;
+
+    class constructor CRC32_PKZIP();
+
+  public
+    constructor Create();
+    procedure TransformBytes(const a_data: THashLibByteArray;
+      a_index, a_length: Int32); override;
+    function Clone(): IHash; override;
+
+  end;
+
+  TCRC32_CASTAGNOLI = class sealed(TCRC32Fast)
+
+  strict private
+
+  const
+    CRC32_CASTAGNOLI_Polynomial = UInt32($82F63B78); // Polynomial Reversed
+    class var
+
+      FCRC32_CASTAGNOLI_Table: THashLibUInt32Array;
+
+    class constructor CRC32_CASTAGNOLI();
+
+  public
+    constructor Create();
+    procedure TransformBytes(const a_data: THashLibByteArray;
+      a_index, a_length: Int32); override;
+    function Clone(): IHash; override;
+
+  end;
+
+implementation
+
+{ TCRC32Fast }
+
+class function TCRC32Fast.Init_CRC_Table(APolynomial: UInt32)
+  : THashLibUInt32Array;
+var
+  LIdx, LJIdx, LKIdx: Int32;
+  LRes: UInt32;
+begin
+  System.SetLength(Result, 16 * 256);
+  for LIdx := 0 to System.Pred(256) do
+  begin
+    LRes := LIdx;
+    for LJIdx := 0 to System.Pred(16) do
+    begin
+      LKIdx := 0;
+      while LKIdx < System.Pred(9) do
+      begin
+        if (LRes and 1) = 1 then
+        begin
+          LRes := APolynomial xor (LRes shr 1)
+        end
+        else
+        begin
+          LRes := LRes shr 1;
+        end;
+        Result[(LJIdx * 256) + LIdx] := LRes;
+        System.Inc(LKIdx);
+      end;
+    end;
+  end;
+end;
+
+procedure TCRC32Fast.LocalCRCCompute(const ACRCTable: THashLibUInt32Array;
+  const AData: THashLibByteArray; AIndex, ALength: Int32);
+var
+  LCRC, LA, LB, LC, LD: UInt32;
+  LCRCTable: THashLibUInt32Array;
+begin
+  LCRC := not FCurrentCRC;
+  LCRCTable := ACRCTable;
+  while ALength >= 16 do
+  begin
+
+    LA := LCRCTable[(3 * 256) + AData[AIndex + 12]] xor LCRCTable
+      [(2 * 256) + AData[AIndex + 13]] xor LCRCTable
+      [(1 * 256) + AData[AIndex + 14]] xor LCRCTable
+      [(0 * 256) + AData[AIndex + 15]];
+
+    LB := LCRCTable[(7 * 256) + AData[AIndex + 8]] xor LCRCTable
+      [(6 * 256) + AData[AIndex + 9]] xor LCRCTable
+      [(5 * 256) + AData[AIndex + 10]] xor LCRCTable
+      [(4 * 256) + AData[AIndex + 11]];
+
+    LC := LCRCTable[(11 * 256) + AData[AIndex + 4]] xor LCRCTable
+      [(10 * 256) + AData[AIndex + 5]] xor LCRCTable
+      [(9 * 256) + AData[AIndex + 6]] xor LCRCTable
+      [(8 * 256) + AData[AIndex + 7]];
+
+    LD := LCRCTable[(15 * 256) + (Byte(LCRC) xor AData[AIndex])] xor LCRCTable
+      [(14 * 256) + (Byte(LCRC shr 8) xor AData[AIndex + 1])] xor LCRCTable
+      [(13 * 256) + (Byte(LCRC shr 16) xor AData[AIndex + 2])] xor LCRCTable
+      [(12 * 256) + ((LCRC shr 24) xor AData[AIndex + 3])];
+
+    LCRC := LD xor LC xor LB xor LA;
+    System.Inc(AIndex, 16);
+    System.Dec(ALength, 16);
+  end;
+
+  System.Dec(ALength);
+  while (ALength >= 0) do
+  begin
+    LCRC := LCRCTable[Byte(LCRC xor AData[AIndex])] xor (LCRC shr 8);
+    System.Inc(AIndex);
+    System.Dec(ALength);
+  end;
+
+  FCurrentCRC := not LCRC;
+end;
+
+constructor TCRC32Fast.Create();
+begin
+  Inherited Create(4, 1);
+end;
+
+procedure TCRC32Fast.Initialize;
+begin
+  FCurrentCRC := 0;
+end;
+
+function TCRC32Fast.TransformFinal: IHashResult;
+begin
+  Result := THashResult.Create(FCurrentCRC);
+  Initialize();
+end;
+
+{ TCRC32_PKZIP }
+
+function TCRC32_PKZIP.Clone(): IHash;
+var
+  HashInstance: TCRC32_PKZIP;
+begin
+  HashInstance := TCRC32_PKZIP.Create();
+  HashInstance.FCurrentCRC := FCurrentCRC;
+  Result := HashInstance as IHash;
+  Result.BufferSize := BufferSize;
+end;
+
+constructor TCRC32_PKZIP.Create;
+begin
+  Inherited Create();
+end;
+
+procedure TCRC32_PKZIP.TransformBytes(const a_data: THashLibByteArray;
+  a_index, a_length: Int32);
+begin
+  LocalCRCCompute(FCRC32_PKZIP_Table, a_data, a_index, a_length);
+end;
+
+class constructor TCRC32_PKZIP.CRC32_PKZIP();
+begin
+  FCRC32_PKZIP_Table := Init_CRC_Table(CRC32_PKZIP_Polynomial);
+end;
+
+{ TCRC32_CASTAGNOLI }
+
+function TCRC32_CASTAGNOLI.Clone(): IHash;
+var
+  HashInstance: TCRC32_CASTAGNOLI;
+begin
+  HashInstance := TCRC32_CASTAGNOLI.Create();
+  HashInstance.FCurrentCRC := FCurrentCRC;
+  Result := HashInstance as IHash;
+  Result.BufferSize := BufferSize;
+end;
+
+constructor TCRC32_CASTAGNOLI.Create;
+begin
+  Inherited Create();
+end;
+
+procedure TCRC32_CASTAGNOLI.TransformBytes(const a_data: THashLibByteArray;
+  a_index, a_length: Int32);
+begin
+  LocalCRCCompute(FCRC32_CASTAGNOLI_Table, a_data, a_index, a_length);
+end;
+
+class constructor TCRC32_CASTAGNOLI.CRC32_CASTAGNOLI();
+begin
+  FCRC32_CASTAGNOLI_Table := Init_CRC_Table(CRC32_CASTAGNOLI_Polynomial);
+end;
+
+end.

+ 1 - 1
src/libraries/hashlib4pascal/HlpFNV64.pas

@@ -65,7 +65,7 @@ begin
   i := a_index;
   i := a_index;
   while a_length > 0 do
   while a_length > 0 do
   begin
   begin
-    Fm_hash := UInt64(Fm_hash * 1099511628211) xor a_data[i];
+    Fm_hash := UInt64(Fm_hash * UInt64(1099511628211)) xor a_data[i];
     System.Inc(i);
     System.Inc(i);
     System.Dec(a_length);
     System.Dec(a_length);
   end;
   end;

+ 9 - 8
src/libraries/hashlib4pascal/HlpHashFactory.pas

@@ -17,6 +17,7 @@ uses
   HlpCRC,
   HlpCRC,
   HlpCRC16,
   HlpCRC16,
   HlpCRC32,
   HlpCRC32,
+  HlpCRC32Fast,
   HlpCRC64,
   HlpCRC64,
   // Hash32 Units //
   // Hash32 Units //
   HlpAP,
   HlpAP,
@@ -136,12 +137,12 @@ type
       class function CreateCRC16_BUYPASS(): IHash; static;
       class function CreateCRC16_BUYPASS(): IHash; static;
 
 
       /// <summary>
       /// <summary>
-      /// PKZIP, polynomial = $04C11DB7
+      /// PKZIP, polynomial = $04C11DB7, reversed = $EDB88320
       /// </summary>
       /// </summary>
       /// <returns></returns>
       /// <returns></returns>
       class function CreateCRC32_PKZIP(): IHash; static;
       class function CreateCRC32_PKZIP(): IHash; static;
       /// <summary>
       /// <summary>
-      /// Castagnoli, polynomial = $1EDC6F41
+      /// Castagnoli, polynomial = $1EDC6F41, reversed = $82F63B78
       /// </summary>
       /// </summary>
       /// <returns></returns>
       /// <returns></returns>
       class function CreateCRC32_CASTAGNOLI(): IHash; static;
       class function CreateCRC32_CASTAGNOLI(): IHash; static;
@@ -480,12 +481,12 @@ end;
 
 
 class function THashFactory.TChecksum.CreateCRC32_CASTAGNOLI: IHash;
 class function THashFactory.TChecksum.CreateCRC32_CASTAGNOLI: IHash;
 begin
 begin
-  Result := TCRC32_CASTAGNOLI.Create();
+  Result := HlpCRC32Fast.TCRC32_CASTAGNOLI.Create();
 end;
 end;
 
 
 class function THashFactory.TChecksum.CreateCRC32_PKZIP: IHash;
 class function THashFactory.TChecksum.CreateCRC32_PKZIP: IHash;
 begin
 begin
-  Result := TCRC32_PKZIP.Create();
+  Result := HlpCRC32Fast.TCRC32_PKZIP.Create();
 end;
 end;
 
 
 class function THashFactory.TChecksum.CreateCRC64(_poly, _Init: UInt64;
 class function THashFactory.TChecksum.CreateCRC64(_poly, _Init: UInt64;
@@ -931,14 +932,14 @@ begin
   Result := TSHA3_512.Create();
   Result := TSHA3_512.Create();
 end;
 end;
 
 
-class function THashFactory.TCrypto.CreateShake_128
-  (a_xof_size_in_bits: Int32): IHash;
+class function THashFactory.TCrypto.CreateShake_128(a_xof_size_in_bits
+  : Int32): IHash;
 begin
 begin
   Result := (TShake_128.Create() as IXOF).SetXOFOutputSize(a_xof_size_in_bits);
   Result := (TShake_128.Create() as IXOF).SetXOFOutputSize(a_xof_size_in_bits);
 end;
 end;
 
 
-class function THashFactory.TCrypto.CreateShake_256
-  (a_xof_size_in_bits: Int32): IHash;
+class function THashFactory.TCrypto.CreateShake_256(a_xof_size_in_bits
+  : Int32): IHash;
 begin
 begin
   Result := (TShake_256.Create() as IXOF).SetXOFOutputSize(a_xof_size_in_bits);
   Result := (TShake_256.Create() as IXOF).SetXOFOutputSize(a_xof_size_in_bits);
 end;
 end;