Browse Source

more refactoring.

Ugochukwu Mmaduekwe 7 years ago
parent
commit
57bead7a42

+ 17 - 15
CryptoLib/src/Security/ClpSecureRandom.pas

@@ -23,9 +23,7 @@ interface
 
 
 uses
 uses
   Math,
   Math,
-{$IFDEF DELPHI}
   SyncObjs,
   SyncObjs,
-{$ENDIF DELPHI}
   SysUtils,
   SysUtils,
   StrUtils,
   StrUtils,
   ClpBits,
   ClpBits,
@@ -55,6 +53,7 @@ type
     FCounter: Int64;
     FCounter: Int64;
     Fmaster: ISecureRandom;
     Fmaster: ISecureRandom;
     FDoubleScale: Double;
     FDoubleScale: Double;
+    FLock: TCriticalSection;
 
 
     class function GetMaster: ISecureRandom; static; inline;
     class function GetMaster: ISecureRandom; static; inline;
 
 
@@ -65,7 +64,8 @@ type
 
 
     class property Master: ISecureRandom read GetMaster;
     class property Master: ISecureRandom read GetMaster;
 
 
-    class constructor SecureRandom();
+    class constructor CreateSecureRandom();
+    class destructor DestroySecureRandom();
 
 
   strict protected
   strict protected
     Fgenerator: IRandomGenerator;
     Fgenerator: IRandomGenerator;
@@ -226,18 +226,14 @@ begin
 end;
 end;
 
 
 class function TSecureRandom.NextCounterValue: Int64;
 class function TSecureRandom.NextCounterValue: Int64;
-{$IFDEF FPC}
-var
-  LCounter: UInt32;
-{$ENDIF FPC}
 begin
 begin
-{$IFDEF DELPHI}
-  Result := TInterlocked.Increment(FCounter);
-{$ELSE}
-  LCounter := UInt32(FCounter);
-  Result := InterLockedIncrement(LCounter);
-  FCounter := Int64(LCounter);
-{$ENDIF DELPHI}
+  FLock.Acquire;
+  try
+    System.Inc(FCounter);
+    Result := FCounter;
+  finally
+    FLock.Release;
+  end;
 end;
 end;
 
 
 function TSecureRandom.NextDouble: Double;
 function TSecureRandom.NextDouble: Double;
@@ -268,9 +264,15 @@ begin
   Result := (Int64(UInt32(NextInt32())) shl 32) or (Int64(UInt32(NextInt32())));
   Result := (Int64(UInt32(NextInt32())) shl 32) or (Int64(UInt32(NextInt32())));
 end;
 end;
 
 
-class constructor TSecureRandom.SecureRandom;
+class constructor TSecureRandom.CreateSecureRandom;
 begin
 begin
   TSecureRandom.Boot;
   TSecureRandom.Boot;
+  FLock := TCriticalSection.Create;
+end;
+
+class destructor TSecureRandom.DestroySecureRandom;
+begin
+  FLock.Free;
 end;
 end;
 
 
 procedure TSecureRandom.SetSeed(seed: Int64);
 procedure TSecureRandom.SetSeed(seed: Int64);

+ 1 - 0
CryptoLib/src/Utils/ClpCryptoLibTypes.pas

@@ -44,6 +44,7 @@ type
   EIOCryptoLibException = class(ECryptoLibException);
   EIOCryptoLibException = class(ECryptoLibException);
   EFormatCryptoLibException = class(ECryptoLibException);
   EFormatCryptoLibException = class(ECryptoLibException);
   ENotImplementedCryptoLibException = class(ECryptoLibException);
   ENotImplementedCryptoLibException = class(ECryptoLibException);
+  ENotSupportedCryptoLibException = class(ECryptoLibException);
   EEndOfStreamCryptoLibException = class(ECryptoLibException);
   EEndOfStreamCryptoLibException = class(ECryptoLibException);
   EStreamOverflowCryptoLibException = class(ECryptoLibException);
   EStreamOverflowCryptoLibException = class(ECryptoLibException);
   EAsn1CryptoLibException = class(ECryptoLibException);
   EAsn1CryptoLibException = class(ECryptoLibException);

+ 88 - 8
CryptoLib/src/Utils/IO/ClpBaseInputStream.pas

@@ -29,7 +29,18 @@ uses
 type
 type
   TBaseInputStream = class abstract(TStream, IBaseInputStream)
   TBaseInputStream = class abstract(TStream, IBaseInputStream)
 
 
+  strict private
+
+    function GetPosition: Int64; inline;
+    procedure SetPosition(const Pos: Int64); inline;
+    procedure SetSize64(const NewSize: Int64); inline;
+
   strict protected
   strict protected
+
+    function GetSize: Int64; override;
+    procedure SetSize(NewSize: LongInt); overload; override;
+    procedure SetSize(const NewSize: Int64); overload; override;
+
     function QueryInterface({$IFDEF FPC}constref {$ELSE}const
     function QueryInterface({$IFDEF FPC}constref {$ELSE}const
 {$ENDIF FPC} IID: TGUID; out Obj): HResult; {$IFDEF MSWINDOWS} stdcall
 {$ENDIF FPC} IID: TGUID; out Obj): HResult; {$IFDEF MSWINDOWS} stdcall
 {$ELSE} cdecl {$ENDIF MSWINDOWS};
 {$ELSE} cdecl {$ENDIF MSWINDOWS};
@@ -39,9 +50,24 @@ type
 {$ENDIF MSWINDOWS};
 {$ENDIF MSWINDOWS};
   public
   public
     function ReadByte: Int32; virtual;
     function ReadByte: Int32; virtual;
-    function Read(Buffer: TCryptoLibByteArray; Offset, Count: Longint): Int32;
+
+    function Read(Buffer: TCryptoLibByteArray; Offset, Count: LongInt): Int32;
 {$IFDEF SUPPORT_TSTREAM_READ_BYTEARRAY_OVERLOAD} override {$ELSE} virtual
 {$IFDEF SUPPORT_TSTREAM_READ_BYTEARRAY_OVERLOAD} override {$ELSE} virtual
 {$ENDIF SUPPORT_TSTREAM_READ_BYTEARRAY_OVERLOAD};
 {$ENDIF SUPPORT_TSTREAM_READ_BYTEARRAY_OVERLOAD};
+
+    function Write(const Buffer: TCryptoLibByteArray;
+      Offset, Count: LongInt): Int32;
+{$IFDEF SUPPORT_TSTREAM_READ_BYTEARRAY_OVERLOAD} override {$ELSE} virtual
+{$ENDIF SUPPORT_TSTREAM_READ_BYTEARRAY_OVERLOAD};
+
+    function Seek(Offset: LongInt; Origin: Word): LongInt; overload; override;
+    function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
+      overload; override;
+
+{$IFNDEF _FIXINSIGHT_}
+    property Size: Int64 read GetSize write SetSize64;
+{$ENDIF}
+    property Position: Int64 read GetPosition write SetPosition;
   end;
   end;
 
 
 implementation
 implementation
@@ -52,6 +78,16 @@ uses
 
 
 { TBaseInputStream }
 { TBaseInputStream }
 
 
+function TBaseInputStream.GetPosition: Int64;
+begin
+  raise ENotSupportedCryptoLibException.Create('');
+end;
+
+function TBaseInputStream.GetSize: Int64;
+begin
+  raise ENotSupportedCryptoLibException.Create('');
+end;
+
 function TBaseInputStream.QueryInterface({$IFDEF FPC}constref {$ELSE}const
 function TBaseInputStream.QueryInterface({$IFDEF FPC}constref {$ELSE}const
 {$ENDIF FPC} IID: TGUID; out Obj): HResult;
 {$ENDIF FPC} IID: TGUID; out Obj): HResult;
 begin
 begin
@@ -78,37 +114,81 @@ begin
   end;
   end;
 end;
 end;
 
 
+function TBaseInputStream.Seek(Offset: LongInt; Origin: Word): LongInt;
+begin
+  result := Seek(Int64(Offset), TSeekOrigin(Origin));
+end;
+
+{$IFNDEF _FIXINSIGHT_}
+
+function TBaseInputStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
+begin
+  raise ENotSupportedCryptoLibException.Create('');
+end;
+
+procedure TBaseInputStream.SetPosition(const Pos: Int64);
+begin
+  raise ENotSupportedCryptoLibException.Create('');
+end;
+
+{$ENDIF}
+
+procedure TBaseInputStream.SetSize(const NewSize: Int64);
+begin
+  SetSize(LongInt(NewSize));
+end;
+
+procedure TBaseInputStream.SetSize(NewSize: LongInt);
+begin
+  raise ENotSupportedCryptoLibException.Create('');
+end;
+
+procedure TBaseInputStream.SetSize64(const NewSize: Int64);
+begin
+  SetSize(NewSize);
+end;
+
 function TBaseInputStream.Read(Buffer: TCryptoLibByteArray;
 function TBaseInputStream.Read(Buffer: TCryptoLibByteArray;
-  Offset, Count: Longint): Int32;
+  Offset, Count: LongInt): Int32;
 var
 var
   &pos, endPoint, b: Int32;
   &pos, endPoint, b: Int32;
 
 
 begin
 begin
-  pos := Offset;
+  Pos := Offset;
   try
   try
     endPoint := Offset + Count;
     endPoint := Offset + Count;
-    while (pos < endPoint) do
+    while (Pos < endPoint) do
     begin
     begin
       b := ReadByte();
       b := ReadByte();
       if (b = -1) then
       if (b = -1) then
       begin
       begin
         break;
         break;
       end;
       end;
-      Buffer[pos] := Byte(b);
-      System.Inc(pos);
+      Buffer[Pos] := Byte(b);
+      System.Inc(Pos);
     end;
     end;
   except
   except
     on e: EIOCryptoLibException do
     on e: EIOCryptoLibException do
     begin
     begin
-      if (pos = Offset) then
+      if (Pos = Offset) then
         raise;
         raise;
     end;
     end;
 
 
   end;
   end;
 
 
-  result := pos - Offset;
+  result := Pos - Offset;
 end;
 end;
 
 
+{$IFNDEF _FIXINSIGHT_}
+
+function TBaseInputStream.Write(const Buffer: TCryptoLibByteArray;
+  Offset, Count: LongInt): Int32;
+begin
+  raise ENotSupportedCryptoLibException.Create('');
+end;
+
+{$ENDIF}
+
 function TBaseInputStream._AddRef: Integer;
 function TBaseInputStream._AddRef: Integer;
 begin
 begin
   result := -1;
   result := -1;