Browse Source

Fix: bug when adding 2 or more operations starting with 0x00

Herman Schoenfeld 8 years ago
parent
commit
fbce76a686
3 changed files with 45 additions and 6 deletions
  1. 2 4
      Units/PascalCoin/UAccounts.pas
  2. 1 0
      Units/PascalCoin/UNode.pas
  3. 42 2
      Units/Utils/UCommon.pas

+ 2 - 4
Units/PascalCoin/UAccounts.pas

@@ -20,7 +20,7 @@ unit UAccounts;
 interface
 interface
 
 
 uses
 uses
-  Classes, UConst, UCrypto, SyncObjs, UThread, UBaseTypes;
+  Classes, UConst, UCrypto, SyncObjs, UThread, UBaseTypes, UCommon;
 {$I config.inc}
 {$I config.inc}
 
 
 Type
 Type
@@ -3243,16 +3243,14 @@ end;
 function TOrderedRawList.Find(const RawData: TRawBytes; var Index: Integer): Boolean;
 function TOrderedRawList.Find(const RawData: TRawBytes; var Index: Integer): Boolean;
 var L, H, I: Integer;
 var L, H, I: Integer;
   c : Integer;
   c : Integer;
-  PRawData : PAnsiChar;
 begin
 begin
   Result := False;
   Result := False;
   L := 0;
   L := 0;
   H := FList.Count - 1;
   H := FList.Count - 1;
-  PRawData := PAnsiChar(RawData);
   while L <= H do
   while L <= H do
   begin
   begin
     I := (L + H) shr 1;
     I := (L + H) shr 1;
-    c := StrComp(PAnsiChar(PRawListData(FList[i])^.RawData),PRawData);
+    c := BinStrComp(PRawListData(FList[i])^.RawData,RawData);
     if C < 0 then L := I + 1 else
     if C < 0 then L := I + 1 else
     begin
     begin
       H := I - 1;
       H := I - 1;

+ 1 - 0
Units/PascalCoin/UNode.pas

@@ -413,6 +413,7 @@ begin
             end;
             end;
           end;
           end;
         end else begin
         end else begin
+          errors := errors + 'Unable to add operation as it has already been added.';
           {$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,Format('AddOperation made before %d/%d: %s',[(j+1),Operations.OperationsCount,ActOp.ToString]));{$ENDIF}
           {$IFDEF HIGHLOG}TLog.NewLog(ltdebug,Classname,Format('AddOperation made before %d/%d: %s',[(j+1),Operations.OperationsCount,ActOp.ToString]));{$ENDIF}
         end;
         end;
       end;
       end;

+ 42 - 2
Units/Utils/UCommon.pas

@@ -7,7 +7,8 @@
   historical operations.
   historical operations.
 
 
   CREDITS:
   CREDITS:
-  [2017-06-29] Herman Schoenfeld ([email protected]): Created unit, added IFF methods
+  [2017-06-29] Herman Schoenfeld ([email protected]): Created unit, added IFF functions
+  [2017-08-10] Herman Schoenfeld ([email protected]): Added String2Hex, BinStrComp functions
 }
 }
 
 
 unit UCommon;
 unit UCommon;
@@ -18,6 +19,12 @@ unit UCommon;
 
 
 interface
 interface
 
 
+{ Converts a string to hexidecimal format }
+function String2Hex(const Buffer: AnsiString): AnsiString;
+
+{ Binary-safe StrComp replacement. StrComp will return 0 for when str1 and str2 both start with NUL character. }
+function BinStrComp(const Str1, Str2 : AnsiString): Integer;
+
 { Language-level tools }
 { Language-level tools }
 function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Cardinal): Cardinal; overload;
 function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Cardinal): Cardinal; overload;
 function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Integer): Integer; overload;
 function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Integer): Integer; overload;
@@ -30,7 +37,40 @@ function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: variant
 implementation
 implementation
 
 
 uses
 uses
-  Classes, SysUtils;
+  Classes, SysUtils, Math;
+
+function String2Hex(const Buffer: AnsiString): AnsiString;
+var
+  n: Integer;
+begin
+  Result := '';
+  for n := 1 to Length(Buffer) do
+    Result := LowerCase(Result + IntToHex(Ord(Buffer[n]), 2));
+end;
+
+
+function BinStrComp(const Str1, Str2: AnsiString): integer;
+var Str1Len, Str2Len, i : Integer;
+begin
+   Str1Len := Length(Str1);
+   Str2Len := Length(Str2);
+   if (Str1Len < Str2Len) then
+     Result := -1
+   else if (Str1Len > Str2Len) then
+     Result := 1
+   else begin
+     Result := 0;
+     For i:= 1 to Str1Len do begin
+       if Str1[i] < Str2[i] then begin
+         Result := -1;
+         break;
+       end else if Str1[i] > Str2[i] then begin
+         Result := 1;
+         break;
+       end
+     end;
+   end;
+End;
 
 
 {%region Language-level tools }
 {%region Language-level tools }
 function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Cardinal): Cardinal;
 function IIF(const ACondition: Boolean; const ATrueResult, AFalseResult: Cardinal): Cardinal;