Skip to content

Commit

Permalink
Remaining Low changes for compiling this with Delphi versions older t…
Browse files Browse the repository at this point in the history
…han XE3.
  • Loading branch information
Markus Humm committed Mar 10, 2021
1 parent 1023f31 commit 9b3d742
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 171 deletions.
400 changes: 232 additions & 168 deletions Demos/Hash_FMX/Hash_FMX.dproj

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Source/DECHashBase.pas
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,13 @@ function TDECHash.CalcString(const Value: string; Format: TDECFormatClass): stri
Result := '';
if Length(Value) > 0 then
begin
{$IF CompilerVersion >= 17.0}
Size := Length(Value) * SizeOf(Value[low(Value)]);
Data := CalcBuffer(Value[low(Value)], Size);
{$ELSE}
Size := Length(Value) * SizeOf(Value[1]);
Data := CalcBuffer(Value[1], Size);
{$ENDIF}
Result := StringOf(ValidFormat(Format).Encode(Data));
end
else
Expand All @@ -745,10 +750,17 @@ function TDECHash.CalcString(const Value: RawByteString; Format: TDECFormatClass
begin
Result := '';
if Length(Value) > 0 then
{$IF CompilerVersion >= 17.0}
result := BytesToRawString(
ValidFormat(Format).Encode(
CalcBuffer(Value[low(Value)],
Length(Value) * SizeOf(Value[low(Value)]))))
{$ELSE}
result := BytesToRawString(
ValidFormat(Format).Encode(
CalcBuffer(Value[1],
Length(Value) * SizeOf(Value[1]))))
{$ENDIF}
else
begin
SetLength(Buf, 0);
Expand Down
4 changes: 4 additions & 0 deletions Source/DECRandom.pas
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,11 @@ function RandomBytes(Size: Integer): TBytes;
function RandomRawByteString(Size: Integer): RawByteString;
begin
SetLength(Result, Size);
{$IF CompilerVersion >= 17.0}
RandomBuffer(Result[Low(Result)], Size);
{$ELSE}
RandomBuffer(Result[1], Size);
{$ENDIF}
end;

function RandomLong: UInt32;
Expand Down
26 changes: 25 additions & 1 deletion Source/DECUtil.pas
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ procedure ProtectStream(Stream: TStream; SizeToProtect: Int64 = 0);
const
BufferSize = 512;
var
Buffer: String;
Buffer: string;
Count, Bytes, Size: Integer;
Position: Integer;
begin
Expand Down Expand Up @@ -595,7 +595,11 @@ procedure ProtectStream(Stream: TStream; SizeToProtect: Int64 = 0);
Bytes := Size;
if Bytes > BufferSize then
Bytes := BufferSize;
{$IF CompilerVersion >= 17.0}
Stream.Write(Buffer[Low(Buffer)], Bytes);
{$ELSE}
Stream.Write(Buffer[1], Bytes);
{$ENDIF}
Dec(Size, Bytes);
end;
end;
Expand All @@ -615,7 +619,11 @@ procedure ProtectString(var Source: string);
if Length(Source) > 0 then
begin
System.UniqueString(Source);
{$IF CompilerVersion >= 17.0}
ProtectBuffer(Pointer(Source)^, Length(Source) * SizeOf(Source[Low(Source)]));
{$ELSE}
ProtectBuffer(Pointer(Source)^, Length(Source) * SizeOf(Source[1]));
{$ENDIF}
Source := '';
end;
end;
Expand All @@ -627,7 +635,11 @@ procedure ProtectString(var Source: RawByteString);
// UniqueString(Source); cannot be called with a RawByteString as there is
// no overload for it, so we need to call our own one.
DECUtilRawByteStringHelper.UniqueString(Source);
{$IF CompilerVersion >= 17.0}
ProtectBuffer(Pointer(Source)^, Length(Source) * SizeOf(Source[Low(Source)]));
{$ELSE}
ProtectBuffer(Pointer(Source)^, Length(Source) * SizeOf(Source[1]));
{$ENDIF}
Source := '';
end;
end;
Expand All @@ -638,7 +650,11 @@ procedure ProtectString(var Source: AnsiString); overload;
if Length(Source) > 0 then
begin
System.UniqueString(Source);
{$IF CompilerVersion >= 17.0}
ProtectBuffer(Pointer(Source)^, Length(Source) * SizeOf(Source[Low(Source)]));
{$ELSE}
ProtectBuffer(Pointer(Source)^, Length(Source) * SizeOf(Source[1]));
{$ENDIF}
Source := '';
end;
end;
Expand All @@ -648,7 +664,11 @@ procedure ProtectString(var Source: WideString); overload;
if Length(Source) > 0 then
begin
System.UniqueString(Source); // for OS <> Win, WideString is not RefCounted on Win
{$IF CompilerVersion >= 17.0}
ProtectBuffer(Pointer(Source)^, Length(Source) * SizeOf(Source[Low(Source)]));
{$ELSE}
ProtectBuffer(Pointer(Source)^, Length(Source) * SizeOf(Source[1]));
{$ENDIF}
Source := '';
end;
end;
Expand All @@ -660,7 +680,11 @@ function BytesToRawString(const Source: TBytes): RawByteString;
if Length(Source) > 0 then
begin
// determine lowest string index for handling of ZeroBasedStrings
{$IF CompilerVersion >= 17.0}
Move(Source[0], Result[Low(result)], Length(Source));
{$ELSE}
Move(Source[0], Result[1], Length(Source));
{$ENDIF}
end;
end;

Expand Down
12 changes: 12 additions & 0 deletions Unit Tests/Tests/TestDECCRC.pas
Original file line number Diff line number Diff line change
Expand Up @@ -579,9 +579,15 @@ procedure TestCRC.TestCRC16Standalone;
begin
if FTestData[i].Input <> '' then
begin
{$IF CompilerVersion >= 17.0}
CRC := CRC16(0,
FTestData[i].Input[low(FTestData[i].Input)],
length(FTestData[i].Input));
{$ELSE}
CRC := CRC16(0,
FTestData[i].Input[1],
length(FTestData[i].Input));
{$ENDIF}

CheckEquals(FTestData[i].CRC, CRC,
'Wrong CRC16Standalone reult for iteration ' + IntToStr(i));
Expand Down Expand Up @@ -705,9 +711,15 @@ procedure TestCRC.TestCRC32Standalone;
begin
if FTestData[i].Input <> '' then
begin
{$IF CompilerVersion >= 17.0}
CRC := CRC32(0,
FTestData[i].Input[low(FTestData[i].Input)],
length(FTestData[i].Input));
{$ELSE}
CRC := CRC32(0,
FTestData[i].Input[1],
length(FTestData[i].Input));
{$ENDIF}

CheckEquals(FTestData[i].CRC, CRC,
'Wrong CRC32Standalone reult for iteration ' + IntToStr(i));
Expand Down
4 changes: 4 additions & 0 deletions Unit Tests/Tests/TestDECCipherFormats.pas
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ function TestTDECCipherFormats.AnsiStringOf(const Bytes: TBytes): AnsiString;
if Assigned(Bytes) then
begin
SetLength(Result, length(Bytes));
{$IF CompilerVersion >= 17.0}
Move(Bytes[0], Result[low(Result)], length(Bytes));
{$ELSE}
Move(Bytes[0], Result[1], length(Bytes));
{$ENDIF}
end
else
Result := '';
Expand Down
8 changes: 8 additions & 0 deletions Unit Tests/Tests/TestDECCipherModes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,19 @@ procedure TestTDECCipherModes.DoTestEncode(Data: array of TTestEntry; Mode: TCip
for n := Low(Dest) to High(Dest) do
Result := Result + IntToHex(Dest[n], 2);

{$IF CompilerVersion >= 17.0}
for n := Low(Result) to High(Result) do
CheckEquals(char(Data[i].OutputHex[n]), Result[n],
IntToStr(n+1) + '. position is wrong. ' +
IntToStr(i) + '. test series. Expected: ' +
string(Data[i].OutputHex) + ' was: ' + Result);
{$ELSE}
for n := 1 to Length(Result) do
CheckEquals(char(Data[i].OutputHex[n]), Result[n],
IntToStr(n+1) + '. position is wrong. ' +
IntToStr(i) + '. test series. Expected: ' +
string(Data[i].OutputHex) + ' was: ' + Result);
{$ENDIF}
end;

finally
Expand Down
15 changes: 13 additions & 2 deletions Unit Tests/Tests/TestDECFormat.pas
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ procedure TestTFormat_Base64.TestIsValidTBytes;
CheckEquals(true, TFormat_Base64.IsValid(SrcBuf));

SetLength(SrcBuf, 1);
for i := 0 to 255 do //Low(Data) to High(Data) do
for i := 0 to 255 do
begin
SrcBuf[0] := i;

Expand Down Expand Up @@ -1029,7 +1029,7 @@ procedure TestTFormat_Base64.TestIsValidTypeless;
CheckEquals(true, TFormat_Base64.IsValid(SrcBuf, 0));

SetLength(SrcBuf, 1);
for i := 0 to 255 do //Low(Data) to High(Data) do
for i := 0 to 255 do
begin
SrcBuf[0] := i;

Expand Down Expand Up @@ -1742,8 +1742,13 @@ procedure TestTFormat_ESCAPE.TestIsValidTypeless;
if i = $5C then
Continue;

{$IF CompilerVersion >= 17.0}
CheckEquals(true, TFormat_ESCAPE.IsValid(RawByteString(chr(i))[low(RawByteString)], 1),
'Failure on ' + chr(i) + ' ');
{$ELSE}
CheckEquals(true, TFormat_ESCAPE.IsValid(RawByteString(chr(i))[1], 1),
'Failure on ' + chr(i) + ' ');
{$ENDIF}
end;

// check hex chars
Expand Down Expand Up @@ -1821,9 +1826,15 @@ procedure TFormatTestsBase.DoTestEncodeDecodeTypeless(EncodeDecodeProc: TEncodeD
begin
if length(TestData[i].Input) > 0 then
begin
{$IF CompilerVersion >= 17.0}
pdata := @TestData[i].Input[low(TestData[i].Input)];

len := length(TestData[i].Input) * SizeOf(TestData[i].Input[low(TestData[i].Input)]);
{$ELSE}
pdata := @TestData[i].Input[1];

len := length(TestData[i].Input) * SizeOf(TestData[i].Input[1]);
{$ENDIF}
end
else
begin
Expand Down
12 changes: 12 additions & 0 deletions Unit Tests/Tests/TestDECFormatBase.pas
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ procedure TestTFormat.TestUpCaseBinary;
i : Integer;
b, exp, res : Byte;
begin
{$IF CompilerVersion >= 17.0}
for i := Low(InputChars) to High(InputChars) do
begin
b := ord(InputChars[i]);
Expand All @@ -266,6 +267,17 @@ procedure TestTFormat.TestUpCaseBinary;

CheckEquals(exp, res);
end;

{$ELSE}
for i := 1 to Length(InputChars) do
begin
b := ord(InputChars[i]);
exp := ord(OutputChars[i]);
res := TDECFormat.UpCaseBinary(b);

CheckEquals(exp, res);
end;
{$ENDIF}
end;

procedure TestTFormat.TestValidFormat;
Expand Down
8 changes: 8 additions & 0 deletions Unit Tests/Tests/TestDECUtil.pas
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,18 @@ procedure TTestBitTwiddling.SwapBytes;
c: Cardinal;
begin
s := Input;
{$IF CompilerVersion >= 17.0}
DECUtil.SwapBytes(s[Low(s)], Length(s));
{$ELSE}
DECUtil.SwapBytes(s[1], Length(s));
{$ENDIF}
CheckEquals(Output, s);

{$IF CompilerVersion >= 17.0}
DECUtil.SwapBytes(s[Low(s)], Length(s));
{$ELSE}
DECUtil.SwapBytes(s[1], Length(s));
{$ENDIF}
CheckEquals(Input, s);

c := 123456789;
Expand Down

0 comments on commit 9b3d742

Please sign in to comment.