Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions SimpleBaseLib/src/Alphabets/SbpAliasedBase32Alphabet.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface
SbpIAliasedBase32Alphabet,
SbpBase32Alphabet;

resourcestring
SErrCharacterNotInAlphabet = 'Character "%s" is not in the alphabet';

type
TAliasedBase32Alphabet = class(TBase32Alphabet, IAliasedBase32Alphabet)
strict private
Expand Down Expand Up @@ -55,8 +58,8 @@ procedure TAliasedBase32Alphabet.MapAlternate(ASource, ADestination: Char);
LResult: Int32;
begin
if not TCodingAlphabet.TryLookup(ReverseLookupTable, ADestination, LResult) then
raise EArgumentSimpleBaseLibException.CreateFmt(
'Character "%s" is not in the alphabet', [ADestination]);
raise EArgumentSimpleBaseLibException.CreateResFmt(
@SErrCharacterNotInAlphabet, [ADestination]);
Map(ASource, LResult);
Map(TCharUtilities.ToAsciiLower(ASource), LResult);
end;
Expand Down
12 changes: 8 additions & 4 deletions SimpleBaseLib/src/Alphabets/SbpCodingAlphabet.pas
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface
SbpICodingAlphabet,
SbpCharUtilities;

resourcestring
SErrAlphabetLengthMismatch = 'Required alphabet length is %d but provided alphabet is %d characters long';
SErrCaseSensitivityConflict = 'Case-sensitivity cannot be selected with an alphabet that contains both cases of the same letter';

type
/// <summary>
/// Base class for ASCII-only coding alphabets with reverse lookup support
Expand Down Expand Up @@ -59,8 +63,8 @@ constructor TCodingAlphabet.Create(ALength: Int32; const AAlphabet: String;

if System.Length(AAlphabet) <> ALength then
begin
raise EArgumentSimpleBaseLibException.CreateFmt
('Required alphabet length is %d but provided alphabet is %d characters long',
raise EArgumentSimpleBaseLibException.CreateResFmt
(@SErrAlphabetLengthMismatch,
[ALength, System.Length(AAlphabet)]);
end;

Expand All @@ -86,8 +90,8 @@ constructor TCodingAlphabet.Create(ALength: Int32; const AAlphabet: String;

if System.Pos(LCounterpart, FValue) <> 0 then
begin
raise EArgumentSimpleBaseLibException.Create
('Case-sensitivity cannot be selected with an alphabet that contains both cases of the same letter');
raise EArgumentSimpleBaseLibException.CreateRes
(@SErrCaseSensitivityConflict);
end;

Map(LCounterpart, LI);
Expand Down
8 changes: 6 additions & 2 deletions SimpleBaseLib/src/Bases/SbpBase16.pas
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ interface
SbpCodingAlphabet,
SbpBase16Alphabet;

resourcestring
SErrInvalidTextLength = 'Invalid text length';
SErrInvalidCharacterInInput = 'Invalid character in input';

type
TBase16 = class(TInterfacedObject, IBase16, IBaseStreamCoder, INonAllocatingBaseCoder)
strict private
Expand Down Expand Up @@ -156,13 +160,13 @@ function TBase16.Decode(const AText: String): TSimpleBaseLibByteArray;
LSafeCount := GetSafeByteCountForDecoding(AText);
if LSafeCount = 0 then
begin
raise EArgumentSimpleBaseLibException.Create('Invalid text length');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInvalidTextLength);
end;

System.SetLength(Result, LSafeCount);
if not InternalDecode(AText, Result, LBytesWritten) then
begin
raise EArgumentSimpleBaseLibException.Create('Invalid character in input');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInvalidCharacterInInput);
end;
end;

Expand Down
8 changes: 6 additions & 2 deletions SimpleBaseLib/src/Bases/SbpBase2.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ interface
SbpIBaseStreamCoder,
SbpStreamUtilities;

resourcestring
SErrInputLengthNotMultipleOf8 = 'Input length must be a multiple of 8';
SErrInvalidBase2Character = 'Invalid Base2 character encountered';

type
TBase2 = class(TInterfacedObject, IBase2, INonAllocatingBaseCoder, IBaseStreamCoder)
strict private
Expand Down Expand Up @@ -90,15 +94,15 @@ function TBase2.Decode(const AText: String): TSimpleBaseLibByteArray;

if (System.Length(AText) mod 8) <> 0 then
begin
raise EArgumentSimpleBaseLibException.Create('Input length must be a multiple of 8');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInputLengthNotMultipleOf8);
end;

LOutputLen := System.Length(AText) div 8;
System.SetLength(Result, LOutputLen);

if not InternalDecode(AText, Result, LOutputLen) then
begin
raise EArgumentSimpleBaseLibException.Create('Invalid Base2 character encountered');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInvalidBase2Character);
end;
end;

Expand Down
31 changes: 17 additions & 14 deletions SimpleBaseLib/src/Bases/SbpBase32.pas
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ interface
SbpBitOperations,
SbpBinaryPrimitives;

resourcestring
SErrOnlyEndPaddingSupported = 'Only encoding alphabets with paddings at the end are supported by this implementation';
SErrInsufficientOutputBuffer = 'Internal error: insufficient output buffer size';
SErrInvalidCharacterInInput = 'Invalid character in input';
SErrUnexpectedDecodeResult = 'Unexpected decode result';
SErrNumberIsNegative = 'Number is negative';
SErrDecodedTextTooLong = 'Decoded text is too long to fit in a buffer';
SErrDecodedOutOfInt64Range = 'Decoded buffer is out of Int64 range';

type
TBase32 = class(TInterfacedObject, IBase32, IBaseStreamCoder, INonAllocatingBaseCoder, INumericBaseCoder)
strict private
Expand Down Expand Up @@ -140,8 +149,7 @@ constructor TBase32.Create(const AAlphabet: IBase32Alphabet);
inherited Create;
if AAlphabet.PaddingPosition <> TPaddingPosition.&End then
begin
raise EArgumentSimpleBaseLibException.Create(
'Only encoding alphabets with paddings at the end are supported by this implementation');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrOnlyEndPaddingSupported);
end;

FAlphabet := AAlphabet;
Expand Down Expand Up @@ -268,8 +276,7 @@ function TBase32.Encode(const ABytes: TSimpleBaseLibByteArray;
System.SetLength(LOutput, LOutputLen);
if not InternalEncode(ABytes, LOutput, APadding, LCharsWritten) then
begin
raise EInvalidOperationSimpleBaseLibException.Create(
'Internal error: insufficient output buffer size');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrInsufficientOutputBuffer);
end;
SetString(Result, PChar(@LOutput[0]), LCharsWritten);
end;
Expand All @@ -293,23 +300,21 @@ function TBase32.Decode(const AText: String): TSimpleBaseLibByteArray;
LDecodeResult := InternalDecode(AText, LTextLen, LOutput, LBytesWritten);
case LDecodeResult of
TDecodeResult.InvalidInput:
raise EArgumentSimpleBaseLibException.Create('Invalid character in input');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInvalidCharacterInInput);
TDecodeResult.OutputOverflow:
raise EInvalidOperationSimpleBaseLibException.Create(
'Internal error: insufficient output buffer size');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrInsufficientOutputBuffer);
TDecodeResult.Success:
Result := System.Copy(LOutput, 0, LBytesWritten);
else
raise EInvalidOperationSimpleBaseLibException.Create(
'Unexpected decode result');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrUnexpectedDecodeResult);
end;
end;

function TBase32.EncodeInt64(const ANumber: Int64): String;
begin
if ANumber < 0 then
begin
raise EArgumentOutOfRangeSimpleBaseLibException.Create('Number is negative');
raise EArgumentOutOfRangeSimpleBaseLibException.CreateRes(@SErrNumberIsNegative);
end;
Result := EncodeUInt64(UInt64(ANumber));
end;
Expand Down Expand Up @@ -350,8 +355,7 @@ function TBase32.DecodeUInt64(const AText: String): UInt64;
end;
if System.Length(LBuffer) > 8 then
begin
raise EInvalidOperationSimpleBaseLibException.Create(
'Decoded text is too long to fit in a buffer');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrDecodedTextTooLong);
end;

System.SetLength(LNewSpan, 8);
Expand Down Expand Up @@ -386,8 +390,7 @@ function TBase32.DecodeInt64(const AText: String): Int64;
LResult := DecodeUInt64(AText);
if LResult > UInt64(High(Int64)) then
begin
raise EArgumentOutOfRangeSimpleBaseLibException.Create(
'Decoded buffer is out of Int64 range');
raise EArgumentOutOfRangeSimpleBaseLibException.CreateRes(@SErrDecodedOutOfInt64Range);
end;
Result := Int64(LResult);
end;
Expand Down
23 changes: 13 additions & 10 deletions SimpleBaseLib/src/Bases/SbpBase45.pas
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ interface
SbpBitOperations,
SbpStreamUtilities;

resourcestring
SErrInsufficientOutputBuffer = 'Internal error: insufficient output buffer size';
SErrInvalidCharacter = 'Invalid character: %s';
SErrInputCorrupt = 'Input buffer is incorrectly encoded or corrupt';
SErrInputIncorrectSize = 'Input buffer is at incorrect size';
SErrUnexpectedDecodeResult = 'Unexpected decode result';

type
TBase45 = class(TInterfacedObject, IBase45, INonAllocatingBaseCoder, IBaseStreamCoder)
strict private
Expand Down Expand Up @@ -334,19 +341,16 @@ function TBase45.Decode(const AText: String): TSimpleBaseLibByteArray;
TDecodeResult.Success:
Result := System.Copy(LOutput, 0, LBytesWritten);
TDecodeResult.InvalidOutputLength:
raise EInvalidOperationSimpleBaseLibException.Create(
'Internal error: insufficient output buffer size');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrInsufficientOutputBuffer);
TDecodeResult.InvalidCharacter:
raise EArgumentSimpleBaseLibException.CreateFmt('Invalid character: %s',
raise EArgumentSimpleBaseLibException.CreateResFmt(@SErrInvalidCharacter,
[LOutcome.InvalidChar]);
TDecodeResult.InvalidInput:
raise EArgumentSimpleBaseLibException.Create(
'Input buffer is incorrectly encoded or corrupt');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInputCorrupt);
TDecodeResult.InvalidInputLength:
raise EArgumentSimpleBaseLibException.Create(
'Input buffer is at incorrect size');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInputIncorrectSize);
else
raise EInvalidOperationSimpleBaseLibException.Create('Unexpected decode result');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrUnexpectedDecodeResult);
end;
end;

Expand All @@ -365,8 +369,7 @@ function TBase45.Encode(const ABytes: TSimpleBaseLibByteArray): String;
System.SetLength(LOutput, LOutputLen);
if not InternalEncode(ABytes, LOutput, LCharsWritten) then
begin
raise EInvalidOperationSimpleBaseLibException.Create(
'Internal error: insufficient output buffer size');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrInsufficientOutputBuffer);
end;
SetString(Result, PChar(@LOutput[0]), LCharsWritten);
end;
Expand Down
24 changes: 14 additions & 10 deletions SimpleBaseLib/src/Bases/SbpBase64.pas
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ interface
SbpBitOperations,
SbpStreamUtilities;

resourcestring
SErrInsufficientOutputBuffer = 'Internal error: insufficient output buffer size';
SErrInvalidBase64Character = 'Invalid Base64 character "%s"';
SErrInvalidBase64Length = 'Invalid Base64 string length';
SErrInvalidBase64Padding = 'Invalid Base64 padding placement';
SErrUnexpectedDecodeResult = 'Unexpected decode result';

type
TBase64 = class(TInterfacedObject, IBase64, INonAllocatingBaseCoder,
IBaseStreamCoder)
Expand Down Expand Up @@ -474,8 +481,7 @@ class function TBase64.AllocatingEncode(const ABytes: TSimpleBaseLibByteArray;
System.SetLength(LOutput, LOutputLen);
if not InternalEncode(ABytes, LOutput, AAlphabetValue, APadding, LCharsWritten) then
begin
raise EInvalidOperationSimpleBaseLibException.Create(
'Internal error: insufficient output buffer size');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrInsufficientOutputBuffer);
end;
SetString(Result, PChar(@LOutput[0]), LCharsWritten);
end;
Expand All @@ -502,18 +508,16 @@ class function TBase64.AllocatingDecode(const AText: String;
TDecodeResult.Success:
Result := System.Copy(LDecodeBuffer, 0, LBytesWritten);
TDecodeResult.InvalidCharacter:
raise EArgumentSimpleBaseLibException.CreateFmt(
'Invalid Base64 character "%s"', [LOutcome.InvalidChar]);
raise EArgumentSimpleBaseLibException.CreateResFmt(
@SErrInvalidBase64Character, [LOutcome.InvalidChar]);
TDecodeResult.InvalidLength:
raise EArgumentSimpleBaseLibException.Create('Invalid Base64 string length');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInvalidBase64Length);
TDecodeResult.InvalidPadding:
raise EArgumentSimpleBaseLibException.Create('Invalid Base64 padding placement');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInvalidBase64Padding);
TDecodeResult.InsufficientOutputBuffer:
raise EInvalidOperationSimpleBaseLibException.Create(
'Internal error: insufficient output buffer size');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrInsufficientOutputBuffer);
else
raise EInvalidOperationSimpleBaseLibException.Create(
'Unexpected decode result');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrUnexpectedDecodeResult);
end;
end;

Expand Down
12 changes: 8 additions & 4 deletions SimpleBaseLib/src/Bases/SbpBase8.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ interface
SbpIBaseStreamCoder,
SbpStreamUtilities;

resourcestring
SErrInvalidBase8Character = 'Invalid Base8 character encountered';
SErrInvalidEncodedTextLength = 'Invalid encoded text length';
SErrUnknownDecodingError = 'Unknown error during decoding - this is a bug';

type
TBase8 = class(TInterfacedObject, IBase8, INonAllocatingBaseCoder, IBaseStreamCoder)
strict private
Expand Down Expand Up @@ -112,12 +117,11 @@ function TBase8.Decode(const AText: String): TSimpleBaseLibByteArray;
Result := System.Copy(Result, 0, LBytesWritten);
end;
TDecodeResult.InvalidCharacter:
raise EArgumentSimpleBaseLibException.Create('Invalid Base8 character encountered');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInvalidBase8Character);
TDecodeResult.InvalidInputLength:
raise EArgumentSimpleBaseLibException.Create('Invalid encoded text length');
raise EArgumentSimpleBaseLibException.CreateRes(@SErrInvalidEncodedTextLength);
else
raise EInvalidOperationSimpleBaseLibException.Create(
'Unknown error during decoding -- this is a bug');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrUnknownDecodingError);
end;
end;

Expand Down
21 changes: 12 additions & 9 deletions SimpleBaseLib/src/Bases/SbpBase85.pas
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ interface
SbpBitOperations,
SbpStreamUtilities;

resourcestring
SErrInsufficientOutputBuffer = 'Internal error: insufficient output buffer size';
SErrInvalidCharacter = 'Invalid character: %s';
SErrInvalidShortcutLocation = 'Invalid location for a shortcut character: %s';
SErrUnexpectedDecodeResult = 'Unexpected decode result';

type
TBase85 = class(TInterfacedObject, IBase85, IBaseStreamCoder, INonAllocatingBaseCoder)
strict private
Expand Down Expand Up @@ -218,8 +224,7 @@ function TBase85.Encode(const ABytes: TSimpleBaseLibByteArray): String;
System.SetLength(LOutput, LOutputLen);
if not InternalEncode(ABytes, LOutput, LCharsWritten) then
begin
raise EInvalidOperationSimpleBaseLibException.Create(
'Internal error: insufficient output buffer size');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrInsufficientOutputBuffer);
end;
SetString(Result, PChar(@LOutput[0]), LCharsWritten);
end;
Expand Down Expand Up @@ -261,16 +266,14 @@ function TBase85.Decode(const AText: String): TSimpleBaseLibByteArray;
TDecodeResult.Success:
Result := System.Copy(LDecodeBuffer, 0, LBytesWritten);
TDecodeResult.InvalidCharacter:
raise EArgumentSimpleBaseLibException.CreateFmt('Invalid character: %s', [LOutcome.InvalidChar]);
raise EArgumentSimpleBaseLibException.CreateResFmt(@SErrInvalidCharacter, [LOutcome.InvalidChar]);
TDecodeResult.InvalidShortcut:
raise EArgumentSimpleBaseLibException.CreateFmt(
'Invalid location for a shortcut character: %s', [LOutcome.InvalidChar]);
raise EArgumentSimpleBaseLibException.CreateResFmt(
@SErrInvalidShortcutLocation, [LOutcome.InvalidChar]);
TDecodeResult.InsufficientOutputBuffer:
raise EInvalidOperationSimpleBaseLibException.Create(
'Internal error: insufficient output buffer size');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrInsufficientOutputBuffer);
else
raise EInvalidOperationSimpleBaseLibException.Create(
'Unexpected decode result');
raise EInvalidOperationSimpleBaseLibException.CreateRes(@SErrUnexpectedDecodeResult);
end;
end;

Expand Down
Loading