Skip to content

Commit b632b14

Browse files
committed
big endian related fixes
1 parent 6de617d commit b632b14

7 files changed

Lines changed: 72 additions & 175 deletions

File tree

SimpleBaseLib.Tests/Delphi.Tests/SimpleBaseLib.Tests.dpr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ uses
7272
SbpBase45 in '..\..\SimpleBaseLib\src\Bases\SbpBase45.pas',
7373
SbpIBase45Alphabet in '..\..\SimpleBaseLib\src\Interfaces\Alphabets\SbpIBase45Alphabet.pas',
7474
SbpIBase45 in '..\..\SimpleBaseLib\src\Interfaces\Bases\SbpIBase45.pas',
75-
SbpPlatformUtilities in '..\..\SimpleBaseLib\src\Utilities\SbpPlatformUtilities.pas',
7675
SbpAliasedBase32Alphabet in '..\..\SimpleBaseLib\src\Alphabets\SbpAliasedBase32Alphabet.pas',
7776
SbpBase32Alphabet in '..\..\SimpleBaseLib\src\Alphabets\SbpBase32Alphabet.pas',
7877
SbpBase32 in '..\..\SimpleBaseLib\src\Bases\SbpBase32.pas',

SimpleBaseLib.Tests/src/Base32/Base32Tests.pas

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ interface
2121
SbpIBase32,
2222
SbpBase32,
2323
SbpBase32Alphabet,
24-
SbpBitOperations,
2524
SimpleBaseLibTestBase;
2625

2726
type
@@ -52,8 +51,6 @@ TTestBase32 = class(TSimpleBaseLibTestCase)
5251
procedure Test_Rfc4648_DecodeUInt64_ReturnsExpectedValues;
5352
procedure Test_Rfc4648_TryDecodeUInt64_ReturnsExpectedValues;
5453
procedure Test_Rfc4648_TryDecodeUInt64_InvalidInput_ReturnsFalse;
55-
procedure Test_Rfc4648_Encode_BigEndianUInt64_ReturnsExpectedValues;
56-
procedure Test_Rfc4648_DecodeUInt64_BigEndian_ReturnsExpectedValues;
5754
procedure Test_Rfc4648_EncodeInt64_Negative_Throws;
5855
procedure Test_Rfc4648_DecodeInt64_ReturnsExpectedValues;
5956
procedure Test_Rfc4648_DecodeInt64_OutOfRange_Throws;
@@ -239,26 +236,6 @@ procedure TTestBase32.Test_Rfc4648_TryDecodeUInt64_InvalidInput_ReturnsFalse;
239236
CheckFalse(TBase32.Rfc4648.TryDecodeUInt64('!@#!@#invalid alphabet!@#!@#', LValue));
240237
end;
241238

242-
procedure TTestBase32.Test_Rfc4648_Encode_BigEndianUInt64_ReturnsExpectedValues;
243-
var
244-
LBigEndian: IBase32;
245-
LValue: UInt64;
246-
begin
247-
LBigEndian := TBase32.Create(TBase32Alphabet.Rfc4648, True);
248-
LValue := TBitOperations.ReverseBytesUInt64($1122334455667788);
249-
CheckEquals('RB3WMVKEGMRBC', LBigEndian.EncodeUInt64(LValue));
250-
end;
251-
252-
procedure TTestBase32.Test_Rfc4648_DecodeUInt64_BigEndian_ReturnsExpectedValues;
253-
var
254-
LBigEndian: IBase32;
255-
LExpected: UInt64;
256-
begin
257-
LBigEndian := TBase32.Create(TBase32Alphabet.Rfc4648, True);
258-
LExpected := TBitOperations.ReverseBytesUInt64($1122334455667788);
259-
CheckEquals(LExpected, LBigEndian.DecodeUInt64('RB3WMVKEGMRBC'));
260-
end;
261-
262239
procedure TTestBase32.Test_Rfc4648_EncodeInt64_Negative_Throws;
263240
begin
264241
try

SimpleBaseLib/src/Bases/SbpBase32.pas

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ interface
1818
SbpStreamUtilities,
1919
SbpCodingAlphabet,
2020
SbpBitOperations,
21-
SbpPlatformUtilities,
2221
SbpBinaryPrimitives;
2322

2423
type
@@ -39,7 +38,6 @@ TBase32 = class(TInterfacedObject, IBase32, IBaseStreamCoder, INonAllocatingBa
3938

4039
var
4140
FAlphabet: IBase32Alphabet;
42-
FIsBigEndian: Boolean;
4341

4442
class var FCrockford: IBase32;
4543
class var FRfc4648: IBase32;
@@ -77,8 +75,7 @@ TBase32 = class(TInterfacedObject, IBase32, IBaseStreamCoder, INonAllocatingBa
7775
public
7876
class constructor Create;
7977

80-
constructor Create(const AAlphabet: IBase32Alphabet); overload;
81-
constructor Create(const AAlphabet: IBase32Alphabet; AIsBigEndian: Boolean); overload;
78+
constructor Create(const AAlphabet: IBase32Alphabet);
8279

8380
class property Crockford: IBase32 read GetCrockford;
8481
class property Rfc4648: IBase32 read GetRfc4648;
@@ -139,11 +136,6 @@ implementation
139136
end;
140137

141138
constructor TBase32.Create(const AAlphabet: IBase32Alphabet);
142-
begin
143-
Create(AAlphabet, not TPlatformUtilities.IsLittleEndian);
144-
end;
145-
146-
constructor TBase32.Create(const AAlphabet: IBase32Alphabet; AIsBigEndian: Boolean);
147139
begin
148140
inherited Create;
149141
if AAlphabet.PaddingPosition <> TPaddingPosition.&End then
@@ -153,7 +145,6 @@ constructor TBase32.Create(const AAlphabet: IBase32Alphabet; AIsBigEndian: Boole
153145
end;
154146

155147
FAlphabet := AAlphabet;
156-
FIsBigEndian := AIsBigEndian;
157148
end;
158149

159150
function TBase32.GetAlphabet: IBase32Alphabet;
@@ -327,9 +318,8 @@ function TBase32.EncodeUInt64(const ANumber: UInt64): String;
327318
const
328319
NumBytes = 8;
329320
var
330-
LBuffer, LSpan: TSimpleBaseLibByteArray;
321+
LBuffer: TSimpleBaseLibByteArray;
331322
LI: Int32;
332-
LTmp: Byte;
333323
begin
334324
if ANumber = 0 then
335325
begin
@@ -340,24 +330,6 @@ function TBase32.EncodeUInt64(const ANumber: UInt64): String;
340330
System.SetLength(LBuffer, NumBytes);
341331
TBinaryPrimitives.WriteUInt64LittleEndian(LBuffer, 0, ANumber);
342332

343-
if FIsBigEndian then
344-
begin
345-
LI := 0;
346-
while (LI < NumBytes) and (LBuffer[LI] = 0) do
347-
begin
348-
Inc(LI);
349-
end;
350-
LSpan := System.Copy(LBuffer, LI, NumBytes - LI);
351-
for LI := 0 to (System.Length(LSpan) div 2) - 1 do
352-
begin
353-
LTmp := LSpan[LI];
354-
LSpan[LI] := LSpan[System.Length(LSpan) - 1 - LI];
355-
LSpan[System.Length(LSpan) - 1 - LI] := LTmp;
356-
end;
357-
Result := Encode(LSpan);
358-
Exit;
359-
end;
360-
361333
LI := NumBytes - 1;
362334
while (LI > 0) and (LBuffer[LI] = 0) do
363335
begin
@@ -369,8 +341,6 @@ function TBase32.EncodeUInt64(const ANumber: UInt64): String;
369341
function TBase32.DecodeUInt64(const AText: String): UInt64;
370342
var
371343
LBuffer, LNewSpan: TSimpleBaseLibByteArray;
372-
LI: Int32;
373-
LTmp: Byte;
374344
begin
375345
LBuffer := Decode(AText);
376346
if System.Length(LBuffer) = 0 then
@@ -388,24 +358,13 @@ function TBase32.DecodeUInt64(const AText: String): UInt64;
388358
TArrayUtilities.Fill<Byte>(LNewSpan, 0, 8, Byte(0));
389359
Move(LBuffer[0], LNewSpan[0], System.Length(LBuffer));
390360

391-
if FIsBigEndian then
392-
begin
393-
for LI := 0 to 3 do
394-
begin
395-
LTmp := LNewSpan[LI];
396-
LNewSpan[LI] := LNewSpan[7 - LI];
397-
LNewSpan[7 - LI] := LTmp;
398-
end;
399-
end;
400-
401361
Result := TBinaryPrimitives.ReadUInt64LittleEndian(LNewSpan, 0);
402362
end;
403363

404364
function TBase32.TryDecodeUInt64(const AText: String; out ANumber: UInt64): Boolean;
405365
var
406366
LOutput: TSimpleBaseLibByteArray;
407-
LBytesWritten, LI: Int32;
408-
LTmp: Byte;
367+
LBytesWritten: Int32;
409368
begin
410369
System.SetLength(LOutput, 8);
411370
TArrayUtilities.Fill<Byte>(LOutput, 0, 8, Byte(0));
@@ -416,16 +375,6 @@ function TBase32.TryDecodeUInt64(const AText: String; out ANumber: UInt64): Bool
416375
Exit;
417376
end;
418377

419-
if FIsBigEndian then
420-
begin
421-
for LI := 0 to 3 do
422-
begin
423-
LTmp := LOutput[LI];
424-
LOutput[LI] := LOutput[7 - LI];
425-
LOutput[7 - LI] := LTmp;
426-
end;
427-
end;
428-
429378
ANumber := TBinaryPrimitives.ReadUInt64LittleEndian(LOutput, 0);
430379
Result := True;
431380
end;

SimpleBaseLib/src/Packages/Delphi/SimpleBaseLib4PascalPackage.dpk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ contains
7979
SbpBase45 in '..\..\Bases\SbpBase45.pas',
8080
SbpIBase45Alphabet in '..\..\Interfaces\Alphabets\SbpIBase45Alphabet.pas',
8181
SbpIBase45 in '..\..\Interfaces\Bases\SbpIBase45.pas',
82-
SbpPlatformUtilities in '..\..\Utilities\SbpPlatformUtilities.pas',
8382
SbpAliasedBase32Alphabet in '..\..\Alphabets\SbpAliasedBase32Alphabet.pas',
8483
SbpBase32Alphabet in '..\..\Alphabets\SbpBase32Alphabet.pas',
8584
SbpBase32 in '..\..\Bases\SbpBase32.pas',

0 commit comments

Comments
 (0)