Browse Source

Refactor change ansisstring[1] access for Low() value

In order to start migrating from AnsiString to TBytes first step is do not use [1] as first array acces. Instead of this use Low() that returns first array value (1 for AnsiString, 0 for TBytes or dynamic array)
PascalCoin 6 years ago
parent
commit
56c21beefd

+ 12 - 12
src/core/UAES.pas

@@ -149,14 +149,14 @@ end;
 class function TAESComp.EVP_Decrypt_AES256(const EncryptedMessage: TRawBytes; APassword: AnsiString; var Decrypted : AnsiString) : Boolean;
 Var bytes_encrypted, bytes_password, bytes_result : TBytes;
 begin
-  SetLength(bytes_encrypted,length(EncryptedMessage));
-  CopyMemory(bytes_encrypted,@EncryptedMessage[1],length(EncryptedMessage));
-  SetLength(bytes_password,length(APassword));
-  CopyMemory(bytes_password,@APassword[1],length(APassword));
+  SetLength(bytes_encrypted,Length(EncryptedMessage));
+  CopyMemory(bytes_encrypted,@EncryptedMessage[Low(EncryptedMessage)],Length(EncryptedMessage));
+  SetLength(bytes_password,Length(APassword));
+  CopyMemory(bytes_password,@APassword[Low(APassword)],Length(APassword));
   Result := EVP_Decrypt_AES256(bytes_encrypted,bytes_password,bytes_result);
   if Result then begin
-    SetLength(Decrypted,length(bytes_result));
-    CopyMemory(@Decrypted[1],bytes_result,length(bytes_result));
+    SetLength(Decrypted,Length(bytes_result));
+    CopyMemory(@Decrypted[Low(Decrypted)],bytes_result,Length(bytes_result));
   end else Decrypted := '';
 end;
 
@@ -214,13 +214,13 @@ end;
 class function TAESComp.EVP_Encrypt_AES256(const TheMessage, APassword: AnsiString): AnsiString;
 Var bytes_message, bytes_password, bytes_result : TBytes;
 begin
-  SetLength(bytes_message,length(TheMessage));
-  CopyMemory(bytes_message,@TheMessage[1],length(TheMessage));
-  SetLength(bytes_password,length(APassword));
-  CopyMemory(bytes_password,@APassword[1],length(APassword));
+  SetLength(bytes_message,Length(TheMessage));
+  CopyMemory(bytes_message,@TheMessage[Low(TheMessage)],Length(TheMessage));
+  SetLength(bytes_password,Length(APassword));
+  CopyMemory(bytes_password,@APassword[Low(APassword)],Length(APassword));
   bytes_result := EVP_Encrypt_AES256(bytes_message,bytes_password);
-  SetLength(Result,length(bytes_result));
-  CopyMemory(@Result[1],bytes_result,length(bytes_result));
+  SetLength(Result,Length(bytes_result));
+  CopyMemory(@Result[Low(Result)],bytes_result,Length(bytes_result));
 end;
 
 class function TAESComp.EVP_Encrypt_AES256(Value, APassword: TBytes): TBytes;

+ 54 - 53
src/core/UAccounts.pas

@@ -23,7 +23,8 @@ unit UAccounts;
 interface
 
 uses
-  Classes, SysUtils, UConst, UCrypto, SyncObjs, UThread, UBaseTypes, UPCOrderedLists;
+  Classes, SysUtils, UConst, UCrypto, SyncObjs, UThread, UBaseTypes,
+  UPCOrderedLists, UPCDataTypes;
 
 {$I config.inc}
 
@@ -543,7 +544,7 @@ Begin
           Raise Exception.Create(Format('%s Integrity on (i)=%d for block account:%d updated on %d > maxBlock %d',[title, i,bl_my.accounts[j].account,bl_my.accounts[j].updated_block,maxBlock]));
         end;
       end;
-      auxH.Replace(i*32,bl_my.block_hash[1],Length(bl_my.block_hash));
+      auxH.Replace(i*32,bl_my.block_hash[Low(bl_my.block_hash)],Length(bl_my.block_hash));
     end;
     if (sb.FBufferBlocksHash.Compare(auxH)<>0) then begin
       Raise Exception.Create(Format('%s Integrity different Buffer Block Hash',[title]));
@@ -638,21 +639,21 @@ end;
 
 class procedure TPascalCoinProtocol.CalcProofOfWork_Part1(const operationBlock: TOperationBlock; out Part1: TRawBytes);
 var ms : TMemoryStream;
-  s : AnsiString;
+  accKeyRaw : TRawBytes;
 begin
   ms := TMemoryStream.Create;
   try
     // Part 1
     ms.Write(operationBlock.block,Sizeof(operationBlock.block)); // Little endian
-    s := TAccountComp.AccountKey2RawString(operationBlock.account_key);
-    ms.WriteBuffer(s[1],length(s));
+    accKeyRaw := TAccountComp.AccountKey2RawString(operationBlock.account_key);
+    ms.WriteBuffer(accKeyRaw[Low(accKeyRaw)],Length(accKeyRaw));
     ms.Write(operationBlock.reward,Sizeof(operationBlock.reward)); // Little endian
     ms.Write(operationBlock.protocol_version,Sizeof(operationBlock.protocol_version)); // Little endian
     ms.Write(operationBlock.protocol_available,Sizeof(operationBlock.protocol_available)); // Little endian
     ms.Write(operationBlock.compact_target,Sizeof(operationBlock.compact_target)); // Little endian
     SetLength(Part1,ms.Size);
     ms.Position:=0;
-    ms.Read(Part1[1],ms.Size);
+    ms.Read(Part1[Low(Part1)],ms.Size);
   finally
     ms.Free;
   end;
@@ -663,13 +664,13 @@ var ms : TMemoryStream;
 begin
   ms := TMemoryStream.Create;
   try
-    ms.WriteBuffer(operationBlock.initial_safe_box_hash[1],length(operationBlock.initial_safe_box_hash));
-    ms.WriteBuffer(operationBlock.operations_hash[1],length(operationBlock.operations_hash));
+    ms.WriteBuffer(operationBlock.initial_safe_box_hash[Low(operationBlock.initial_safe_box_hash)],length(operationBlock.initial_safe_box_hash));
+    ms.WriteBuffer(operationBlock.operations_hash[Low(operationBlock.operations_hash)],length(operationBlock.operations_hash));
     // Note about fee: Fee is stored in 8 bytes, but only digest first 4 low bytes
     ms.Write(operationBlock.fee,4);
     SetLength(Part3,ms.Size);
     ms.Position := 0;
-    ms.ReadBuffer(Part3[1],ms.Size);
+    ms.ReadBuffer(Part3[Low(Part3)],ms.Size);
   finally
     ms.Free;
   end;
@@ -677,23 +678,23 @@ end;
 
 class procedure TPascalCoinProtocol.CalcProofOfWork(const operationBlock: TOperationBlock; out PoW: TRawBytes);
 var ms : TMemoryStream;
-  s : AnsiString;
+  accKeyRaw : TRawBytes;
 begin
   ms := TMemoryStream.Create;
   try
     // Part 1
     ms.Write(operationBlock.block,Sizeof(operationBlock.block)); // Little endian
-    s := TAccountComp.AccountKey2RawString(operationBlock.account_key);
-    ms.WriteBuffer(s[1],length(s));
+    accKeyRaw := TAccountComp.AccountKey2RawString(operationBlock.account_key);
+    ms.WriteBuffer(accKeyRaw[Low(accKeyRaw)],Length(accKeyRaw));
     ms.Write(operationBlock.reward,Sizeof(operationBlock.reward)); // Little endian
     ms.Write(operationBlock.protocol_version,Sizeof(operationBlock.protocol_version)); // Little endian
     ms.Write(operationBlock.protocol_available,Sizeof(operationBlock.protocol_available)); // Little endian
     ms.Write(operationBlock.compact_target,Sizeof(operationBlock.compact_target)); // Little endian
     // Part 2
-    ms.WriteBuffer(operationBlock.block_payload[1],length(operationBlock.block_payload));
+    ms.WriteBuffer(operationBlock.block_payload[Low(operationBlock.block_payload)],Length(operationBlock.block_payload));
     // Part 3
-    ms.WriteBuffer(operationBlock.initial_safe_box_hash[1],length(operationBlock.initial_safe_box_hash));
-    ms.WriteBuffer(operationBlock.operations_hash[1],length(operationBlock.operations_hash));
+    ms.WriteBuffer(operationBlock.initial_safe_box_hash[Low(operationBlock.initial_safe_box_hash)],length(operationBlock.initial_safe_box_hash));
+    ms.WriteBuffer(operationBlock.operations_hash[Low(operationBlock.operations_hash)],length(operationBlock.operations_hash));
     // Note about fee: Fee is stored in 8 bytes (Int64), but only digest first 4 low bytes
     ms.Write(operationBlock.fee,4);
     ms.Write(operationBlock.timestamp,4);
@@ -773,7 +774,7 @@ end;
 
 class function TPascalCoinProtocol.TargetFromCompact(encoded: Cardinal; protocol_version : Integer): TRawBytes;
 Var
-  nbits, high, offset, i: Cardinal;
+  nbits, offset, i: Cardinal;
   bn: TBigNum;
   raw : TRawBytes;
 begin
@@ -819,9 +820,9 @@ begin
     bn.LShift(256 - nbits - 25);
     raw := bn.RawValue;
     SetLength(Result,32);
-    FillChar(Result[1],32,0);
-    for i:=1 to Length(raw) do begin
-      result[i+32-length(raw)] := raw[i];
+    FillChar(Result[Low(Result)],32,0);
+    for i:=Low(raw) to High(raw) do begin
+      result[i+32-Length(raw)] := raw[i];
     end;
   Finally
     bn.Free;
@@ -843,8 +844,8 @@ begin
     raise Exception.Create('Invalid target to compact: '+TCrypto.ToHexaString(target)+' ('+inttostr(length(target))+')');
   end;
   SetLength(raw,32);
-  FillChar(raw[1],32,0);
-  for j:=1 to length(target) do begin
+  FillChar(raw[Low(raw)],32,0);
+  for j:=Low(target) to High(target) do begin
     raw[j+32-length(target)] := target[j];
   end;
   target := raw;
@@ -903,33 +904,33 @@ class function TStreamOp.SaveStreamToRaw(Stream: TStream): TRawBytes;
 begin
   SetLength(Result,Stream.Size);
   Stream.Position:=0;
-  Stream.ReadBuffer(Result[1],Stream.Size);
+  Stream.ReadBuffer(Result[Low(Result)],Stream.Size);
 end;
 
 class procedure TStreamOp.LoadStreamFromRaw(Stream: TStream; const raw: TRawBytes);
 begin
-  Stream.WriteBuffer(raw[1],Length(raw));
+  Stream.WriteBuffer(raw[Low(raw)],Length(raw));
 end;
 
 class function TStreamOp.ReadAnsiString(Stream: TStream; var value: AnsiString): Integer;
 Var
-  l: Word;
+  w: Word;
 begin
   if Stream.Size - Stream.Position < 2 then begin
     value := '';
     Result := -1;
     exit;
   end;
-  Stream.Read(l, 2);
-  if Stream.Size - Stream.Position < l then begin
+  Stream.Read(w, 2);
+  if Stream.Size - Stream.Position < w then begin
     Stream.Position := Stream.Position - 2; // Go back!
     value := '';
     Result := -1;
     exit;
   end;
-  SetLength(value, l);
-  Stream.ReadBuffer(value[1], l);
-  Result := l+2;
+  SetLength(value, w);
+  Stream.ReadBuffer(value[Low(value)], w);
+  Result := w+2;
 end;
 
 class function TStreamOp.WriteAccountKey(Stream: TStream; const value: TAccountKey): Integer;
@@ -941,18 +942,18 @@ end;
 
 class function TStreamOp.WriteAnsiString(Stream: TStream; const value: AnsiString): Integer;
 Var
-  l: Word;
+  w: Word;
 begin
   if (Length(value)>(256*256)) then begin
     TLog.NewLog(lterror,Classname,'Invalid stream size! '+Inttostr(Length(value)));
     raise Exception.Create('Invalid stream size! '+Inttostr(Length(value)));
   end;
 
-  l := Length(value);
-  Stream.Write(l, 2);
-  if (l > 0) then
-    Stream.WriteBuffer(value[1], Length(value));
-  Result := l+2;
+  w := Length(value);
+  Stream.Write(w, 2);
+  if (w > 0) then
+    Stream.WriteBuffer(value[Low(value)], Length(value));
+  Result := w+2;
 end;
 
 { TAccountComp }
@@ -987,7 +988,7 @@ begin
         TStreamOp.WriteAccountKey(ms,AccountInfo.new_publicKey);
         SetLength(dest,ms.Size);
         ms.Position := 0;
-        ms.Read(dest[1],ms.Size);
+        ms.Read(dest[Low(dest)],ms.Size);
       Finally
         ms.Free;
       end;
@@ -1045,7 +1046,7 @@ begin
     TStreamOp.WriteAccountKey(s,account);
     SetLength(dest,s.Size);
     s.Position := 0;
-    s.Read(dest[1],s.Size);
+    s.Read(dest[Low(dest)],s.Size);
   finally
     s.Free;
   end;
@@ -1429,13 +1430,13 @@ class procedure TAccountComp.RawString2AccountInfo(const rawaccstr: TRawBytes; v
 Var ms : TMemoryStream;
   w : Word;
 begin
-  if length(rawaccstr)=0 then begin
+  if Length(rawaccstr)=0 then begin
     dest := CT_AccountInfo_NUL;
     exit;
   end;
   ms := TMemoryStream.Create;
   Try
-    ms.WriteBuffer(rawaccstr[1],length(rawaccstr));
+    ms.WriteBuffer(rawaccstr[Low(rawaccstr)],Length(rawaccstr));
     ms.Position := 0;
     If ms.Read(w,SizeOf(w))<>SizeOf(w) then exit;
     case w of
@@ -1477,7 +1478,7 @@ begin
   end;
   ms := TMemoryStream.Create;
   try
-    ms.WriteBuffer(rawaccstr[1],length(rawaccstr));
+    ms.WriteBuffer(rawaccstr[Low(rawaccstr)],Length(rawaccstr));
     ms.Position := 0;
     TStreamOp.ReadAccountKey(ms,dest);
   finally
@@ -1868,7 +1869,7 @@ begin
     ToTMemBlockAccount(Result,Pblock^);
     FBlockAccountsList.Add(Pblock);
   end;
-  FBufferBlocksHash.Add(Result.block_hash[1],Length(Result.block_hash));
+  FBufferBlocksHash.Add(Result.block_hash[Low(Result.block_hash)],Length(Result.block_hash));
   Inc(FTotalBalance,Int64(blockChain.reward + blockChain.fee));
   Dec(FTotalFee, Int64(blockChain.fee));
   If (length(accs_miner)>0) then begin
@@ -2012,7 +2013,7 @@ begin
       for i := Low(block.accounts) to High(block.accounts) do begin
         ms.Write(block.accounts[i].account,4);  // Little endian
         raw := TAccountComp.AccountInfo2RawString(block.accounts[i].accountInfo);
-        ms.WriteBuffer(raw[1],length(raw)); // Raw bytes
+        ms.WriteBuffer(raw[Low(raw)],Length(raw)); // Raw bytes
         ms.Write(block.accounts[i].balance,SizeOf(Uint64));  // Little endian
         ms.Write(block.accounts[i].updated_block,4);  // Little endian
         ms.Write(block.accounts[i].n_operation,4); // Little endian
@@ -2024,13 +2025,13 @@ begin
       for i := Low(block.accounts) to High(block.accounts) do begin
         ms.Write(block.accounts[i].account,4);  // Little endian
         raw := TAccountComp.AccountInfo2RawString(block.accounts[i].accountInfo);
-        ms.WriteBuffer(raw[1],length(raw)); // Raw bytes
+        ms.WriteBuffer(raw[Low(raw)],Length(raw)); // Raw bytes
         ms.Write(block.accounts[i].balance,SizeOf(Uint64));  // Little endian
         ms.Write(block.accounts[i].updated_block,4);  // Little endian
         ms.Write(block.accounts[i].n_operation,4); // Little endian
         // Use new Protocol 2 fields
-        If length(block.accounts[i].name)>0 then begin
-          ms.WriteBuffer(block.accounts[i].name[1],length(block.accounts[i].name));
+        If Length(block.accounts[i].name)>0 then begin
+          ms.WriteBuffer(block.accounts[i].name[Low(block.accounts[i].name)],Length(block.accounts[i].name));
         end;
         ms.Write(block.accounts[i].account_type,2);
       end;
@@ -3035,8 +3036,8 @@ begin
     // Offsets zone
     posOffsetZone:=Stream.Position;
     SetLength(raw,(totalBlocks+1)*4); // Last position = end
-    FillChar(raw[1],length(raw),0);
-    Stream.WriteBuffer(raw[1],length(raw));
+    FillChar(raw[Low(raw)],Length(raw),0);
+    Stream.WriteBuffer(raw[Low(raw)],Length(raw));
     setLength(offsets,totalBlocks+1); // c = total blocks  - Last position = offset to end
     // Blocks zone
     for iblock := FromBlock to ToBlock do begin
@@ -3093,7 +3094,7 @@ begin
     // Read Source Offset zone
     posOffsetZoneSource := Source.Position;
     SetLength(offsetsSource,(sbHeader.endBlock-sbHeader.startBlock)+2);
-    Source.Read(offsetsSource[0],4*length(offsetsSource));
+    Source.Read(offsetsSource[0],4*Length(offsetsSource));
     // DEST STREAM:
     // Init dest stream
     // Header zone
@@ -3101,8 +3102,8 @@ begin
     // Offsets zone
     posOffsetZoneDest:=Dest.Position;
     SetLength(raw,(destTotalBlocks+1)*4); // Cardinal = 4 bytes for each block + End position
-    FillChar(raw[1],length(raw),0);
-    Dest.WriteBuffer(raw[1],length(raw));
+    FillChar(raw[Low(raw)],Length(raw),0);
+    Dest.WriteBuffer(raw[Low(raw)],Length(raw));
     setLength(offsetsDest,destTotalBlocks+1);
     // Blocks zone
     posBlocksZoneDest := Dest.Position;
@@ -3123,7 +3124,7 @@ begin
     end;
     offsetsDest[high(offsetsDest)] := posFinal - posOffsetZoneDest;
 
-    Dest.WriteBuffer(offsetsDest[0],length(offsetsDest)*4);
+    Dest.WriteBuffer(offsetsDest[0],Length(offsetsDest)*4);
     Dest.Position := posFinal;
     Source.Position := offsetsSource[High(offsetsSource)] + posOffsetZoneSource;
     TStreamOp.ReadAnsiString(Source,raw);
@@ -3741,8 +3742,8 @@ begin
     {$ENDIF}
   end;
   // Update buffer block hash
-  j := (length(blockAccount.block_hash)*(iBlock));  // j in 0,32,64...
-  FBufferBlocksHash.Replace(j,blockAccount.block_hash[1],32);
+  j := (Length(blockAccount.block_hash)*(iBlock));  // j in 0,32,64...
+  FBufferBlocksHash.Replace(j,blockAccount.block_hash[Low(blockAccount.block_hash)],32);
 
   FTotalBalance := FTotalBalance - (Int64(lastbalance)-Int64(newBalance));
   FTotalFee := FTotalFee + (Int64(lastbalance)-Int64(newBalance));

+ 19 - 19
src/core/UBaseTypes.pas

@@ -75,7 +75,7 @@ Type
     class procedure T32BytesToRawBytes(const source : T32Bytes; var dest : TDynRawBytes); overload;
     class function T32BytesToRawBytes(const source : T32Bytes) : TDynRawBytes; overload;
     class function TRawBytesTo32Left0Padded(const source : TDynRawBytes) : T32Bytes;
-    class function Copy(const source : T32bytes; start, length : Integer) : ShortString; overload;
+    class function Copy(const source : T32bytes; astart, alength : Integer) : ShortString; overload;
     class function Copy(const source : T256RawBytes; var dest : T256RawBytes) : ShortString; overload;
     class function To256RawBytes(const source : TRawBytes) : T256RawBytes; overload;
     class procedure To256RawBytes(const source : TRawBytes; var dest : T256RawBytes); overload;
@@ -145,17 +145,17 @@ begin
   end;
 end;
 
-class function TBaseType.Copy(const source: T32bytes; start, length: Integer): ShortString;
+class function TBaseType.Copy(const source: T32bytes; astart, alength: Integer): ShortString;
 begin
-  if (length+start)>32 then raise Exception.Create('ERROR DEV 20170601-1');
-  SetLength(Result,length);
-  move(source[start],Result[1],length);
+  if (alength+astart)>32 then raise Exception.Create('ERROR DEV 20170601-1');
+  SetLength(Result,alength);
+  move(source[astart],Result[Low(Result)],alength);
 end;
 
 class function TBaseType.Copy(const source: T256RawBytes; var dest: T256RawBytes): ShortString;
 var i : Integer;
 begin
-  SetLength(dest,length(source));
+  SetLength(dest,Length(source));
   for i:=0 to high(dest) do begin
     dest[i] := source[i];
   end;
@@ -163,38 +163,38 @@ end;
 
 class function TBaseType.To256RawBytes(const source: TRawBytes): T256RawBytes;
 begin
-  SetLength(Result,length(source));
-  move(source[1],Result[0],length(source));
+  SetLength(Result,Length(source));
+  move(source[Low(source)],Result[0],Length(source));
 end;
 
 class procedure TBaseType.To256RawBytes(const source: TRawBytes; var dest: T256RawBytes);
 begin
-  SetLength(dest,length(source));
-  move(source[1],dest[0],length(source));
+  SetLength(dest,Length(source));
+  move(source[Low(source)],dest[0],Length(source));
 end;
 
 class function TBaseType.ToRawBytes(const source: T256RawBytes): TRawBytes;
 begin
-  SetLength(Result,length(source));
-  move(source[0],Result[1],length(source));
+  SetLength(Result,Length(source));
+  move(source[0],Result[Low(Result)],Length(source));
 end;
 
 class procedure TBaseType.ToRawBytes(const source: T256RawBytes; var dest: TRawBytes);
 begin
-  SetLength(dest,length(source));
-  move(source[0],dest[1],length(source));
+  SetLength(dest,Length(source));
+  move(source[0],dest[Low(dest)],Length(source));
 end;
 
 class function TBaseType.ToRawBytes(const source: T32Bytes): TRawBytes;
 begin
-  SetLength(Result,length(source));
-  move(source[0],Result[1],length(source));
+  SetLength(Result,Length(source));
+  move(source[0],Result[Low(Result)],Length(source));
 end;
 
 class procedure TBaseType.ToRawBytes(const source: T32Bytes; var dest: TRawBytes);
 begin
-  SetLength(dest,length(source));
-  move(source[0],dest[1],length(source));
+  SetLength(dest,Length(source));
+  move(source[0],dest[Low(dest)],Length(source));
 end;
 
 class function TBaseType.To32Bytes(const source: TRawBytes): T32Bytes;
@@ -208,7 +208,7 @@ begin
   FillByte(dest[0],32,0);
   i := length(source);
   if (i>32) then i:=32;
-  move(source[1],dest[0],i);
+  move(source[Low(source)],dest[0],i);
 end;
 
 class procedure TBaseType.Fill0(var dest: T32Bytes);

+ 14 - 13
src/core/UBlockChain.pas

@@ -23,7 +23,8 @@ unit UBlockChain;
 interface
 
 uses
-  Classes, UCrypto, UAccounts, ULog, UThread, SyncObjs, UBaseTypes, SysUtils;
+  Classes, UCrypto, UAccounts, ULog, UThread, SyncObjs, UBaseTypes, SysUtils,
+  UPCDataTypes;
 {$I config.inc}
 
 {
@@ -1210,9 +1211,9 @@ begin
     Calc_Digest_Parts;
   end;
   FStreamPoW.Position := 0;
-  FStreamPoW.WriteBuffer(FDigest_Part1[1],length(FDigest_Part1));
-  FStreamPoW.WriteBuffer(FDigest_Part2_Payload[1],length(FDigest_Part2_Payload));
-  FStreamPoW.WriteBuffer(FDigest_Part3[1],length(FDigest_Part3));
+  FStreamPoW.WriteBuffer(FDigest_Part1[Low(FDigest_Part1)],Length(FDigest_Part1));
+  FStreamPoW.WriteBuffer(FDigest_Part2_Payload[Low(FDigest_Part2_Payload)],Length(FDigest_Part2_Payload));
+  FStreamPoW.WriteBuffer(FDigest_Part3[Low(FDigest_Part3)],Length(FDigest_Part3));
   FStreamPoW.Write(FOperationBlock.timestamp,4);
   FStreamPoW.Write(FOperationBlock.nonce,4);
   if CT_ACTIVATE_RANDOMHASH_V4 AND (FOperationBlock.protocol_version >= CT_PROTOCOL_4) then
@@ -2788,8 +2789,8 @@ begin
     try
       SaveOpToStream(ms,False);
       ms.Position := 0;
-      setlength(Result,ms.Size);
-      ms.ReadBuffer(Result[1],ms.Size);
+      SetLength(Result,ms.Size);
+      ms.ReadBuffer(Result[Low(Result)],ms.Size);
     finally
       ms.Free;
     end;
@@ -2877,17 +2878,17 @@ begin
   block :=0;
   account :=0;
   n_operation :=0;
-  md160Hash:='';
+  SetLength(md160Hash,0);
   if length(operationHash)<>32 then exit;
   ms := TMemoryStream.Create;
   try
-    ms.Write(operationHash[1],length(operationHash));
+    ms.Write(operationHash[Low(operationHash)],Length(operationHash));
     ms.position := 0;
     ms.Read(block,4);
     ms.Read(account,4);
     ms.Read(n_operation,4);
     SetLength(md160Hash, 20);
-    ms.ReadBuffer(md160Hash[1], 20);
+    ms.ReadBuffer(md160Hash[Low(md160Hash)], 20);
     Result := true;
   finally
     ms.free;
@@ -3029,10 +3030,10 @@ begin
     // BUG IN PREVIOUS VERSIONS: (1.5.5 and prior)
     // Function DoRipeMD160 returned a 40 bytes value, because data was converted in hexa string!
     // So, here we used only first 20 bytes, and WHERE HEXA values, so only 16 diff values per 2 byte!
-    ms.WriteBuffer(TCrypto.DoRipeMD160_HEXASTRING(op.GetBufferForOpHash(False))[1],20);
+    ms.WriteBuffer(TCrypto.DoRipeMD160_HEXASTRING(op.GetBufferForOpHash(False))[Low(TRawBytes)],20);
     SetLength(Result,ms.size);
     ms.Position:=0;
-    ms.Read(Result[1],ms.size);
+    ms.Read(Result[Low(Result)],ms.size);
   finally
     ms.Free;
   end;
@@ -3058,10 +3059,10 @@ begin
     _o := op.N_Operation;
     ms.Write(_a,4);    // Save Account (4 bytes)
     ms.Write(_o,4);    // Save N_Operation (4 bytes)
-    ms.WriteBuffer(TCrypto.DoRipeMD160AsRaw(op.GetBufferForOpHash(True))[1],20); // Calling GetBufferForOpHash(TRUE) is the same than data used for Sha256
+    ms.WriteBuffer(TCrypto.DoRipeMD160AsRaw(op.GetBufferForOpHash(True))[Low(TRawBytes)],20); // Calling GetBufferForOpHash(TRUE) is the same than data used for Sha256
     SetLength(Result,ms.size);
     ms.Position:=0;
-    ms.Read(Result[1],ms.size);
+    ms.Read(Result[Low(Result)],ms.size);
   finally
     ms.Free;
   end;

+ 30 - 44
src/core/UCrypto.pas

@@ -25,25 +25,11 @@ unit UCrypto;
 interface
 
 uses
-  Classes, SysUtils, UOpenSSL, UOpenSSLdef, URandomHash, UBaseTypes;
+  Classes, SysUtils, UOpenSSL, UOpenSSLdef, URandomHash, UBaseTypes, UPCDataTypes;
 
 Type
   ECryptoException = Class(Exception);
 
-  PRawBytes = ^TRawBytes;
-
-  TECDSA_SIG = record
-     r: TRawBytes;
-     s: TRawBytes;
-  end; { record }
-
-  TECDSA_Public = record
-     EC_OpenSSL_NID : Word;
-     x: TRawBytes;
-     y: TRawBytes;
-  end;
-  PECDSA_Public = ^TECDSA_Public;
-
   TECPrivateKey = Class
   private
     FPrivateKey: PEC_KEY;
@@ -193,11 +179,11 @@ begin
   Try
     ms.Write(FEC_OpenSSL_NID,sizeof(FEC_OpenSSL_NID));
     SetLength(aux,BN_num_bytes(EC_KEY_get0_private_key(FPrivateKey)));
-    BN_bn2bin(EC_KEY_get0_private_key(FPrivateKey),@aux[1]);
+    BN_bn2bin(EC_KEY_get0_private_key(FPrivateKey),@aux[Low(aux)]);
     TStreamOp.WriteAnsiString(ms,aux);
     SetLength(Result,ms.Size);
     ms.Position := 0;
-    ms.Read(Result[1],ms.Size);
+    ms.Read(Result[Low(Result)],ms.Size);
   Finally
     ms.Free;
   End;
@@ -225,9 +211,9 @@ begin
   Try
     EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(FPrivateKey),EC_KEY_get0_public_key(FPrivateKey),BNx,BNy,ctx);
     SetLength(Result.x,BN_num_bytes(BNx));
-    BN_bn2bin(BNx,@Result.x[1]);
+    BN_bn2bin(BNx,@Result.x[Low(Result.x)]);
     SetLength(Result.y,BN_num_bytes(BNy));
-    BN_bn2bin(BNy,@Result.y[1]);
+    BN_bn2bin(BNy,@Result.y[Low(Result.y)]);
   Finally
     BN_CTX_free(ctx);
     BN_free(BNx);
@@ -250,11 +236,11 @@ begin
   Result := Nil;
   ms := TMemoryStream.Create;
   Try
-    ms.WriteBuffer(raw[1],length(raw));
+    ms.WriteBuffer(raw[Low(raw)],Length(raw));
     ms.Position := 0;
     if ms.Read(ECID,sizeof(ECID))<>sizeof(ECID) then exit;
     If TStreamOp.ReadAnsiString(ms,aux)<0 then exit;
-    BNx := BN_bin2bn(PAnsiChar(aux),length(aux),nil);
+    BNx := BN_bin2bn(PAnsiChar(aux),Length(aux),nil);
     if assigned(BNx) then begin
       try
         PAC := BN_bn2hex(BNx);
@@ -369,7 +355,7 @@ class procedure TCrypto.DoDoubleSha256(p: PAnsiChar; plength: Cardinal; out Resu
 Var PS : PAnsiChar;
 begin
   If length(ResultSha256)<>32 then SetLength(ResultSha256,32);
-  PS := @ResultSha256[1];
+  PS := @ResultSha256[Low(ResultSha256)];
   SHA256(p,plength,PS);
   SHA256(PS,32,PS);
 end;
@@ -399,7 +385,7 @@ class function TCrypto.DoRipeMD160AsRaw(p: PAnsiChar; plength: Cardinal): TRawBy
 Var PS : PAnsiChar;
 begin
   SetLength(Result,20);
-  PS := @Result[1];
+  PS := @Result[Low(Result)];
   RIPEMD160(p,plength,PS);
 end;
 
@@ -407,7 +393,7 @@ class function TCrypto.DoRipeMD160AsRaw(const TheMessage: AnsiString): TRawBytes
 Var PS : PAnsiChar;
 begin
   SetLength(Result,20);
-  PS := @Result[1];
+  PS := @Result[Low(Result)];
   RIPEMD160(PAnsiChar(TheMessage),Length(TheMessage),PS);
 end;
 
@@ -415,7 +401,7 @@ class function TCrypto.DoSha256(p: PAnsiChar; plength: Cardinal): TRawBytes;
 Var PS : PAnsiChar;
 begin
   SetLength(Result,32);
-  PS := @Result[1];
+  PS := @Result[Low(Result)];
   SHA256(p,plength,PS);
 end;
 
@@ -423,7 +409,7 @@ class function TCrypto.DoSha256(const TheMessage: AnsiString): TRawBytes;
 Var PS : PAnsiChar;
 begin
   SetLength(Result,32);
-  PS := @Result[1];
+  PS := @Result[Low(Result)];
   SHA256(PAnsiChar(TheMessage),Length(TheMessage),PS);
 end;
 
@@ -435,7 +421,7 @@ class procedure TCrypto.DoSha256(const TheMessage: AnsiString; out ResultSha256:
 Var PS : PAnsiChar;
 begin
   If length(ResultSha256)<>32 then SetLength(ResultSha256,32);
-  PS := @ResultSha256[1];
+  PS := @ResultSha256[Low(ResultSha256)];
   SHA256(PAnsiChar(TheMessage),Length(TheMessage),PS);
 end;
 
@@ -450,12 +436,12 @@ begin
 
     i := BN_num_bytes(PECS^._r);
     SetLength(Result.r,i);
-    p := @Result.r[1];
+    p := @Result.r[Low(Result.r)];
     i := BN_bn2bin(PECS^._r,p);
 
     i := BN_num_bytes(PECS^._s);
     SetLength(Result.s,i);
-    p := @Result.s[1];
+    p := @Result.s[Low(Result.s)];
     i := BN_bn2bin(PECS^._s,p);
   Finally
     ECDSA_SIG_free(PECS);
@@ -533,13 +519,13 @@ Var P : PAnsiChar;
  i : Integer;
 begin
   Result := '';
-  if ((length(HexaString) MOD 2)<>0) Or (length(HexaString)=0) then exit;
-  SetLength(result,length(HexaString) DIV 2);
-  P := @Result[1];
+  if ((Length(HexaString) MOD 2)<>0) Or (Length(HexaString)=0) then exit;
+  SetLength(result,Length(HexaString) DIV 2);
+  P := @Result[Low(Result)];
   lc := LowerCase(HexaString);
-  i := HexToBin(PAnsiChar(@lc[1]),P,length(Result));
-  if (i<>(length(HexaString) DIV 2)) then begin
-    TLog.NewLog(lterror,Classname,'Invalid HEXADECIMAL string result '+inttostr(i)+'<>'+inttostr(length(HexaString) DIV 2)+': '+HexaString);
+  i := HexToBin(PAnsiChar(@lc[Low(lc)]),P,Length(Result));
+  if (i<>(Length(HexaString) DIV 2)) then begin
+    TLog.NewLog(lterror,Classname,'Invalid HEXADECIMAL string result '+inttostr(i)+'<>'+inttostr(Length(HexaString) DIV 2)+': '+HexaString);
     Result := '';
   end;
 end;
@@ -550,16 +536,16 @@ Var P : PAnsiChar;
  i : Integer;
 begin
   Result := False; raw := '';
-  if ((length(HexaString) MOD 2)<>0) then Exit;
-  if (length(HexaString)=0) then begin
+  if ((Length(HexaString) MOD 2)<>0) then Exit;
+  if (Length(HexaString)=0) then begin
     Result := True;
     exit;
   end;
-  SetLength(raw,length(HexaString) DIV 2);
-  P := @raw[1];
+  SetLength(raw,Length(HexaString) DIV 2);
+  P := @raw[Low(raw)];
   lc := LowerCase(HexaString);
-  i := HexToBin(PAnsiChar(@lc[1]),P,length(raw));
-  Result := (i = (length(HexaString) DIV 2));
+  i := HexToBin(PAnsiChar(@lc[Low(lc)]),P,Length(raw));
+  Result := (i = (Length(HexaString) DIV 2));
 end;
 
 class procedure TCrypto.InitCrypto;
@@ -663,7 +649,7 @@ begin
   SetLength(LInput, plength);
   Move(p^, LInput[0], plength);
   LResult := TRandomHashFast.Compute(LInput);
-  Move(LResult[0], ResultSha256[1], 32);
+  Move(LResult[0], ResultSha256[Low(ResultSha256)], 32);
 end;
 
 class procedure TCrypto.DoRandomHash(AFastHasher : TRandomHashFast; p : PAnsiChar; plength : Cardinal; out ResultSha256 : TRawBytes);
@@ -675,7 +661,7 @@ begin
   SetLength(LInput, plength);
   Move(p^, LInput[0], plength);
   LResult := AFastHasher.Hash(LInput);
-  Move(LResult[0], ResultSha256[1], 32);
+  Move(LResult[0], ResultSha256[Low(ResultSha256)], 32);
 end;
 
 { TBigNum }
@@ -780,7 +766,7 @@ Var p : PAnsiChar;
 begin
   i := BN_num_bytes(FBN);
   SetLength(Result,i);
-  p := @Result[1];
+  p := @Result[Low(Result)];
   i := BN_bn2bin(FBN,p);
 end;
 

+ 6 - 6
src/core/UECIES.pas

@@ -43,7 +43,7 @@ unit UECIES;
 
 interface
 
-Uses UOpenSSLdef, UOpenSSL, UCrypto, ULog, UConst, UBaseTypes;
+Uses UOpenSSLdef, UOpenSSL, UCrypto, ULog, UConst, UBaseTypes, UPCDataTypes;
 
 Const CT_Max_Bytes_To_Encrypt = 32000;
 
@@ -270,7 +270,7 @@ begin
         // Initialize the cipher with the envelope key.
         if (EVP_EncryptInit_ex(pcipher,EVP_aes_256_cbc,nil,@envelope_key,@iv)<>1) or
           (EVP_CIPHER_CTX_set_padding(pcipher,0)<>1) or
-          (EVP_EncryptUpdate(pcipher,body,body_length,@MessageToEncrypt[1],
+          (EVP_EncryptUpdate(pcipher,body,body_length,@MessageToEncrypt[Low(MessageToEncrypt)],
             Length(MessageToEncrypt) - (Length(MessageToEncrypt) MOD block_length))<>1) then begin
               TLog.NewLog(lterror,'ECIES',Format('An error occurred while trying to secure the data using the chosen symmetric cipher. {error = %s}',
               [ERR_error_string(ERR_get_error(),nil)]));
@@ -291,7 +291,7 @@ begin
           {$ELSE}
           FillMemory(@block,length(block),0);
           {$ENDIF}
-          CopyMemory(@block,Pointer(PtrInt(@MessageToEncrypt[1])+body_length),Length(MessageToEncrypt)-body_length);
+          CopyMemory(@block,Pointer(PtrInt(@MessageToEncrypt[Low(MessageToEncrypt)])+body_length),Length(MessageToEncrypt)-body_length);
           // Advance the body pointer to the location of the remaining space, and calculate just how much room is still available.
           body := Pointer(PtrInt(body)+body_length);
           body_length := secure_body_length(cryptex) - body_length;
@@ -353,7 +353,7 @@ begin
         {$ENDIF}
       End;
       SetLength(Result,secure_total_length(cryptex));
-      CopyMemory(@Result[1],cryptex,length(Result));
+      CopyMemory(@Result[Low(Result)],cryptex,Length(Result));
     finally
       secure_free(cryptex);
     end;
@@ -411,7 +411,7 @@ var
 Begin
   Result := false;
   Decrypted := '';
-  cryptex := Psecure_t(@MessageToDecrypt[1]);
+  cryptex := Psecure_t(@MessageToDecrypt[Low(MessageToDecrypt)]);
   // Make sure we are generating enough key material for the symmetric ciphers.
   key_length := EVP_CIPHER_key_length(EVP_aes_256_cbc);
   if (key_length*2>SHA512_DIGEST_LENGTH) then begin
@@ -510,7 +510,7 @@ Begin
         exit;
       end;
       SetLength(Decrypted,secure_orig_length(cryptex));
-      CopyMemory(@Decrypted[1],output,length(Decrypted));
+      CopyMemory(@Decrypted[Low(Decrypted)],output,length(Decrypted));
       Result := true;
     finally
       {$IFDEF OpenSSL10}

+ 1 - 1
src/core/ULog.pas

@@ -163,7 +163,7 @@ begin
     if assigned(FFileStream) And (logType in FSaveTypes) then begin
       if TThread.CurrentThread.ThreadID=MainThreadID then tid := ' MAIN:' else tid:=' TID:';
       s := FormatDateTime('yyyy-mm-dd hh:nn:ss.zzz',now)+tid+IntToHex(PtrInt(TThread.CurrentThread.ThreadID),8)+' ['+CT_LogType[logtype]+'] <'+sender+'> '+logtext+#13#10;
-      FFileStream.Write(s[1],length(s));
+      FFileStream.Write(s[Low(s)],Length(s));
     end;
     if Assigned(FOnInThreadNewLog) then begin
       FOnInThreadNewLog(logtype,now,TThread.CurrentThread.ThreadID,sender,logtext);

+ 58 - 58
src/core/UOpTransaction.pas

@@ -22,7 +22,7 @@ unit UOpTransaction;
 
 interface
 
-Uses UCrypto, UBlockChain, Classes, UAccounts, UBaseTypes;
+Uses UCrypto, UBlockChain, Classes, UAccounts, UBaseTypes, UPCDataTypes;
 
 Type
   // Operations Type
@@ -689,7 +689,7 @@ begin
     if (current_protocol<=CT_PROTOCOL_3) then begin
       Stream.Position := 0;
       setlength(Result,Stream.Size);
-      Stream.ReadBuffer(Result[1],Stream.Size);
+      Stream.ReadBuffer(Result[Low(Result)],Stream.Size);
     end else begin
       b := OpType;
       Stream.Write(b,1);
@@ -895,20 +895,20 @@ begin
       ms.Write(FData.target,Sizeof(FData.target));
       ms.Write(FData.amount,Sizeof(FData.amount));
       ms.Write(FData.fee,Sizeof(FData.fee));
-      if length(FData.payload)>0 then
-        ms.WriteBuffer(FData.payload[1],length(FData.payload));
+      if Length(FData.payload)>0 then
+        ms.WriteBuffer(FData.payload[Low(FData.payload)],Length(FData.payload));
       ms.Write(FData.public_key.EC_OpenSSL_NID,Sizeof(FData.public_key.EC_OpenSSL_NID));
-      if length(FData.public_key.x)>0 then
-        ms.WriteBuffer(FData.public_key.x[1],length(FData.public_key.x));
-      if length(FData.public_key.y)>0 then
-        ms.WriteBuffer(FData.public_key.y[1],length(FData.public_key.y));
-      if length(FData.sign.r)>0 then
-        ms.WriteBuffer(FData.sign.r[1],length(FData.sign.r));
-      if length(FData.sign.s)>0 then
-        ms.WriteBuffer(FData.sign.s[1],length(FData.sign.s));
+      if Length(FData.public_key.x)>0 then
+        ms.WriteBuffer(FData.public_key.x[Low(FData.public_key.x)],Length(FData.public_key.x));
+      if Length(FData.public_key.y)>0 then
+        ms.WriteBuffer(FData.public_key.y[Low(FData.public_key.y)],Length(FData.public_key.y));
+      if Length(FData.sign.r)>0 then
+        ms.WriteBuffer(FData.sign.r[Low(FData.sign.r)],Length(FData.sign.r));
+      if Length(FData.sign.s)>0 then
+        ms.WriteBuffer(FData.sign.s[Low(FData.sign.s)],Length(FData.sign.s));
       SetLength(Result,ms.Size);
       ms.Position := 0;
-      ms.ReadBuffer(Result[1],ms.Size);
+      ms.ReadBuffer(Result[Low(Result)],ms.Size);
     finally
       ms.Free;
     end;
@@ -1122,26 +1122,26 @@ begin
     ms.Write(FData.target,Sizeof(FData.target));
     ms.Write(FData.amount,Sizeof(FData.amount));
     ms.Write(FData.fee,Sizeof(FData.fee));
-    if length(FData.payload)>0 then
-      ms.WriteBuffer(FData.payload[1],length(FData.payload));
+    if Length(FData.payload)>0 then
+      ms.WriteBuffer(FData.payload[Low(FData.payload)],Length(FData.payload));
     ms.Write(FData.public_key.EC_OpenSSL_NID,Sizeof(FData.public_key.EC_OpenSSL_NID));
-    if length(FData.public_key.x)>0 then
-      ms.WriteBuffer(FData.public_key.x[1],length(FData.public_key.x));
-    if length(FData.public_key.y)>0 then
-      ms.WriteBuffer(FData.public_key.y[1],length(FData.public_key.y));
+    if Length(FData.public_key.x)>0 then
+      ms.WriteBuffer(FData.public_key.x[Low(FData.public_key.x)],Length(FData.public_key.x));
+    if Length(FData.public_key.y)>0 then
+      ms.WriteBuffer(FData.public_key.y[Low(FData.public_key.y)],Length(FData.public_key.y));
     if FData.opTransactionStyle=buy_account then begin
       ms.Write(FData.AccountPrice,Sizeof(FData.AccountPrice));
       ms.Write(FData.SellerAccount,Sizeof(FData.SellerAccount));
       ms.Write(FData.new_accountkey.EC_OpenSSL_NID,Sizeof(FData.new_accountkey.EC_OpenSSL_NID));
-      if length(FData.new_accountkey.x)>0 then
-        ms.WriteBuffer(FData.new_accountkey.x[1],length(FData.new_accountkey.x));
-      if length(FData.new_accountkey.y)>0 then
-        ms.WriteBuffer(FData.new_accountkey.y[1],length(FData.new_accountkey.y));
+      if Length(FData.new_accountkey.x)>0 then
+        ms.WriteBuffer(FData.new_accountkey.x[Low(FData.new_accountkey.x)],Length(FData.new_accountkey.x));
+      if Length(FData.new_accountkey.y)>0 then
+        ms.WriteBuffer(FData.new_accountkey.y[Low(FData.new_accountkey.y)],Length(FData.new_accountkey.y));
     end;
     if (current_protocol<=CT_PROTOCOL_3) then begin
       ms.Position := 0;
       SetLength(Result,ms.Size);
-      ms.ReadBuffer(Result[1],ms.Size);
+      ms.ReadBuffer(Result[Low(Result)],ms.Size);
     end else begin
       b := OpType;
       ms.Write(b,1);
@@ -1307,23 +1307,23 @@ begin
       ms.Write(FData.account_signer,Sizeof(FData.account_signer)); //Protocol 1 does not allow signer/target. signer=target always
       ms.Write(FData.n_operation,Sizeof(FData.n_operation));
       ms.Write(FData.fee,Sizeof(FData.fee));
-      if length(FData.payload)>0 then
-        ms.WriteBuffer(FData.payload[1],length(FData.payload));
+      if Length(FData.payload)>0 then
+        ms.WriteBuffer(FData.payload[Low(FData.payload)],Length(FData.payload));
       ms.Write(FData.public_key.EC_OpenSSL_NID,Sizeof(FData.public_key.EC_OpenSSL_NID));
-      if length(FData.public_key.x)>0 then
-        ms.WriteBuffer(FData.public_key.x[1],length(FData.public_key.x));
-      if length(FData.public_key.y)>0 then
-        ms.WriteBuffer(FData.public_key.y[1],length(FData.public_key.y));
+      if Length(FData.public_key.x)>0 then
+        ms.WriteBuffer(FData.public_key.x[Low(FData.public_key.x)],Length(FData.public_key.x));
+      if Length(FData.public_key.y)>0 then
+        ms.WriteBuffer(FData.public_key.y[Low(FData.public_key.y)],Length(FData.public_key.y));
       s := TAccountComp.AccountKey2RawString(FData.new_accountkey);
-      if length(s)>0 then
-        ms.WriteBuffer(s[1],length(s));
-      if length(FData.sign.r)>0 then
-        ms.WriteBuffer(FData.sign.r[1],length(FData.sign.r));
-      if length(FData.sign.s)>0 then
-        ms.WriteBuffer(FData.sign.s[1],length(FData.sign.s));
+      if Length(s)>0 then
+        ms.WriteBuffer(s[Low(s)],Length(s));
+      if Length(FData.sign.r)>0 then
+        ms.WriteBuffer(FData.sign.r[Low(FData.sign.r)],Length(FData.sign.r));
+      if Length(FData.sign.s)>0 then
+        ms.WriteBuffer(FData.sign.s[Low(FData.sign.s)],Length(FData.sign.s));
       ms.Position := 0;
       setlength(Result,ms.Size);
-      ms.ReadBuffer(Result[1],ms.Size);
+      ms.ReadBuffer(Result[Low(Result)],ms.Size);
     finally
       ms.Free;
     end;
@@ -1456,20 +1456,20 @@ begin
     if (FData.account_signer<>FData.account_target) then ms.Write(FData.account_target,Sizeof(FData.account_target));
     ms.Write(FData.n_operation,Sizeof(FData.n_operation));
     ms.Write(FData.fee,Sizeof(FData.fee));
-    if length(FData.payload)>0 then
-      ms.WriteBuffer(FData.payload[1],length(FData.payload));
+    if Length(FData.payload)>0 then
+      ms.WriteBuffer(FData.payload[Low(FData.payload)],Length(FData.payload));
     ms.Write(FData.public_key.EC_OpenSSL_NID,Sizeof(FData.public_key.EC_OpenSSL_NID));
-    if length(FData.public_key.x)>0 then
-      ms.WriteBuffer(FData.public_key.x[1],length(FData.public_key.x));
-    if length(FData.public_key.y)>0 then
-      ms.WriteBuffer(FData.public_key.y[1],length(FData.public_key.y));
+    if Length(FData.public_key.x)>0 then
+      ms.WriteBuffer(FData.public_key.x[Low(FData.public_key.x)],Length(FData.public_key.x));
+    if Length(FData.public_key.y)>0 then
+      ms.WriteBuffer(FData.public_key.y[Low(FData.public_key.y)],Length(FData.public_key.y));
     s := TAccountComp.AccountKey2RawString(FData.new_accountkey);
-    if length(s)>0 then
-      ms.WriteBuffer(s[1],length(s));
+    if Length(s)>0 then
+      ms.WriteBuffer(s[Low(s)],Length(s));
     if (current_protocol<=CT_PROTOCOL_3) then begin
       ms.Position := 0;
-      setlength(Result,ms.Size);
-      ms.ReadBuffer(Result[1],ms.Size);
+      SetLength(Result,ms.Size);
+      ms.ReadBuffer(Result[Low(Result)],ms.Size);
     end else begin
       b := OpType;
       ms.Write(b,1);
@@ -1549,7 +1549,7 @@ begin
       ms.Write(FData.fee,Sizeof(FData.fee));
       ms.Position := 0;
       SetLength(Result,ms.Size);
-      ms.ReadBuffer(Result[1],ms.Size);
+      ms.ReadBuffer(Result[Low(Result)],ms.Size);
     finally
       ms.Free;
     end;
@@ -1975,21 +1975,21 @@ begin
     ms.Write(FData.account_price,Sizeof(FData.account_price));
     ms.Write(FData.account_to_pay,Sizeof(FData.account_to_pay));
     ms.Write(FData.fee,Sizeof(FData.fee));
-    if length(FData.payload)>0 then
-      ms.WriteBuffer(FData.payload[1],length(FData.payload));
+    if Length(FData.payload)>0 then
+      ms.WriteBuffer(FData.payload[Low(FData.payload)],Length(FData.payload));
     ms.Write(FData.public_key.EC_OpenSSL_NID,Sizeof(FData.public_key.EC_OpenSSL_NID));
-    if length(FData.public_key.x)>0 then
-      ms.WriteBuffer(FData.public_key.x[1],length(FData.public_key.x));
-    if length(FData.public_key.y)>0 then
-      ms.WriteBuffer(FData.public_key.y[1],length(FData.public_key.y));
+    if Length(FData.public_key.x)>0 then
+      ms.WriteBuffer(FData.public_key.x[Low(FData.public_key.x)],Length(FData.public_key.x));
+    if Length(FData.public_key.y)>0 then
+      ms.WriteBuffer(FData.public_key.y[Low(FData.public_key.y)],Length(FData.public_key.y));
     s := TAccountComp.AccountKey2RawString(FData.new_public_key);
-    if length(s)>0 then
-      ms.WriteBuffer(s[1],length(s));
+    if Length(s)>0 then
+      ms.WriteBuffer(s[Low(s)],Length(s));
     ms.Write(FData.locked_until_block,Sizeof(FData.locked_until_block));
     if (current_protocol<=CT_PROTOCOL_3) then begin
       ms.Position := 0;
-      setlength(Result,ms.Size);
-      ms.ReadBuffer(Result[1],ms.Size);
+      SetLength(Result,ms.Size);
+      ms.ReadBuffer(Result[Low(Result)],ms.Size);
     end else begin
       b := OpType;
       ms.Write(b,1);

+ 2 - 2
src/core/UPoolMinerThreads.pas

@@ -705,7 +705,7 @@ begin
     if Not TPoolMinerThread.UseRandomHash(FMinerValuesForWork.version) then begin
       repeat // Prepare payload_start with SHA256 compatible last chunk size
         digest := FMinerValuesForWork.part1 + FMinerValuesForWork.payload_start + FMinerValuesForWork.part3 + '00000000';
-        ok := CanBeModifiedOnLastChunk(length(digest),npos);
+        ok := CanBeModifiedOnLastChunk(Length(digest),npos);
         if (not ok) then FMinerValuesForWork.payload_start:=FMinerValuesForWork.payload_start+'.';
       until (Ok);
     end else begin
@@ -728,7 +728,7 @@ begin
         if (FCPUs>0) then cpu.FMaxNOnce:=nextmin + (Cardinal($FFFFFFFF) DIV FCPUs) - 1
         else cpu.FMaxNOnce:= nextmin + (Cardinal($FFFFFFFF)) - 1;
         nextmin := cpu.FMaxNOnce+1;
-        cpu.FDigestStreamMsg.WriteBuffer(digest[1],length(digest));
+        cpu.FDigestStreamMsg.WriteBuffer(digest[Low(digest)],Length(digest));
       finally
         cpu.Flock.Release;
       end;

+ 4 - 4
src/core/URPC.pas

@@ -25,7 +25,7 @@ interface
 Uses UThread, ULog, UConst, UNode, UAccounts, UCrypto, UBlockChain,
   UNetProtocol, UOpTransaction, UWallet, UTime, UAES, UECIES, UTxMultiOperation,
   UJSONFunctions, classes, blcksock, synsock, IniFiles, Variants, math, UBaseTypes, UOpenSSL,
-  UPCOrderedLists,
+  UPCOrderedLists, UPCDataTypes,
   UNetProtection;
 
 Const
@@ -390,7 +390,7 @@ Begin
     OperationsHashTree.SaveOperationsHashTreeToStream(ms,false);
     ms.Position := 0;
     SetLength(raw,ms.Size);
-    ms.ReadBuffer(raw[1],ms.Size);
+    ms.ReadBuffer(raw[Low(raw)],ms.Size);
     Result := TCrypto.ToHexaString(raw);
   Finally
     ms.Free;
@@ -669,7 +669,7 @@ begin
             Fsock.sendstring(headers[n] + CRLF);
         end;
         if Fsock.lasterror = 0 then begin
-          FSock.SendBuffer(addr(jsonresponsetxt[1]),Length(jsonresponsetxt));
+          FSock.SendBuffer(addr(jsonresponsetxt[Low(jsonresponsetxt)]),Length(jsonresponsetxt));
         end;
       end;
       _RPCServer.AddRPCLog(FSock.GetRemoteSinIP+':'+InttoStr(FSock.GetRemoteSinPort),'Method:'+methodName+' Params:'+paramsTxt+' '+Inttostr(errNum)+':'+errDesc+' Time:'+FormatFloat('0.000',(TPlatform.GetElapsedMilliseconds(tc)/1000)));
@@ -724,7 +724,7 @@ function TRPCProcess.ProcessMethod(const method: String; params: TPCJSONObject;
     end;
     ms := TMemoryStream.Create;
     Try
-      ms.WriteBuffer(raw[1],length(raw));
+      ms.WriteBuffer(raw[Low(raw)],Length(raw));
       ms.Position := 0;
       OperationsHashTree := TOperationsHashTree.Create;
       if (raw<>'') then begin

+ 1 - 1
src/core/USha256.pas

@@ -169,7 +169,7 @@ begin
     Stream.Position:= 0;
     m := Sha256HashToRaw(CalcSHA256(Stream));
     Stream.size := 0;
-    Stream.WriteBuffer(m[1],32);
+    Stream.WriteBuffer(m[Low(m)],32);
     Stream.Position:= 0;
     Result := CalcSHA256(Stream);
   finally

+ 3 - 3
src/core/UTxMultiOperation.pas

@@ -23,7 +23,7 @@ unit UTxMultiOperation;
 interface
 
 uses
-  Classes, SysUtils, UCrypto, UBlockChain, UAccounts, UBaseTypes;
+  Classes, SysUtils, UCrypto, UBlockChain, UAccounts, UBaseTypes, UPCDataTypes;
 
 Type
 
@@ -1081,8 +1081,8 @@ begin
     end;
     if (current_protocol<=CT_PROTOCOL_3) then begin
       ms.Position := 0;
-      setlength(Result,ms.Size);
-      ms.ReadBuffer(Result[1],ms.Size);
+      SetLength(Result,ms.Size);
+      ms.ReadBuffer(Result[Low(Result)],ms.Size);
     end else begin
       b := OpType;
       ms.Write(b,1);

+ 2 - 1
src/core/UWallet.pas

@@ -23,7 +23,8 @@ unit UWallet;
 interface
 
 uses
-  Classes, USettings, UBlockChain, UAccounts, UCrypto, UBaseTypes, UCommon;
+  Classes, USettings, UBlockChain, UAccounts, UCrypto, UBaseTypes, UCommon,
+  UPCDataTypes;
 
 Type
   TWalletKey = Record