소스 검색

- Add clone method for MurmurHash3_x86_32

this gives the ability to clone the hash internal state.
Ugochukwu Mmaduekwe 7 년 전
부모
커밋
8777cfa7c5

+ 9 - 0
src/libraries/hashlib4pascal/HlpHash.pas

@@ -24,6 +24,7 @@ resourcestring
   // SInvalidHashSize = '"HashSize" Must Be Greater Than Zero';
   SUnAssignedStream = 'Input Stream Is Unassigned';
   SFileNotExist = 'Specified File Not Found';
+  SNotYetImplemented = 'Not Yet Implemented For "%s"';
 
 type
   THash = class abstract(TInterfacedObject, IHash)
@@ -77,6 +78,8 @@ type
     procedure Initialize(); virtual; abstract;
     function TransformFinal(): IHashResult; virtual; abstract;
 
+    function Clone(): IHash; virtual;
+
     property BufferSize: Int32 read GetBufferSize write SetBufferSize;
   end;
 
@@ -230,6 +233,12 @@ begin
 
 end;
 
+function THash.Clone(): IHash;
+begin
+  raise ENotImplementedHashLibException.CreateResFmt
+    (@SNotYetImplemented, [Name]);
+end;
+
 function THash.ComputeBytes(const a_data: THashLibByteArray): IHashResult;
 begin
   Initialize();

+ 1 - 0
src/libraries/hashlib4pascal/HlpHashLibTypes.pas

@@ -25,6 +25,7 @@ type
   EArgumentNilHashLibException = class(EHashLibException);
   EArgumentOutOfRangeHashLibException = class(EHashLibException);
   ENullReferenceHashLibException = class(EHashLibException);
+  ENotImplementedHashLibException = class(EHashLibException);
   EUnsupportedTypeHashLibException = class(EHashLibException);
 
 {$IFDEF HAS_UNITSCOPE}

+ 4 - 1
src/libraries/hashlib4pascal/HlpIHash.pas

@@ -49,7 +49,10 @@ type
 
     function TransformFinal(): IHashResult;
 
-    procedure TransformString(const a_data: String; const a_encoding: TEncoding);
+    function Clone(): IHash;
+
+    procedure TransformString(const a_data: String;
+      const a_encoding: TEncoding);
     procedure TransformStream(const a_stream: TStream; a_length: Int64 = -1);
     procedure TransformFile(const a_file_name: String; a_from: Int64 = 0;
       a_length: Int64 = -1);

+ 16 - 0
src/libraries/hashlib4pascal/HlpMurmurHash3_x86_32.pas

@@ -16,6 +16,7 @@ uses
   HlpIHashInfo,
   HlpNullable,
   HlpHash,
+  HlpIHash,
   HlpHashResult,
   HlpIHashResult,
   HlpBits;
@@ -57,6 +58,7 @@ type
     procedure TransformBytes(const a_data: THashLibByteArray;
       a_index, a_length: Int32); override;
     function TransformFinal: IHashResult; override;
+    function Clone(): IHash; override;
     property KeyLength: TNullableInteger read GetKeyLength;
     property Key: THashLibByteArray read GetKey write SetKey;
 
@@ -66,6 +68,20 @@ implementation
 
 { TMurmurHash3_x86_32 }
 
+function TMurmurHash3_x86_32.Clone: IHash;
+var
+  HashInstance: TMurmurHash3_x86_32;
+begin
+  HashInstance := TMurmurHash3_x86_32.Create();
+  HashInstance.Fm_key := Fm_key;
+  HashInstance.Fm_h := Fm_h;
+  HashInstance.Fm_total_length := Fm_total_length;
+  HashInstance.Fm_idx := Fm_idx;
+  HashInstance.Fm_buf := System.Copy(Fm_buf);
+  result := HashInstance as IHash;
+  result.BufferSize := BufferSize;
+end;
+
 constructor TMurmurHash3_x86_32.Create;
 begin
   Inherited Create(4, 4);