You've already forked lazarus-ccr
Invalid characters handling at the end of base 64 encoded strings. Reported by Birger Jansen, thanks.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@705 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -69,7 +69,7 @@ const
|
|||||||
IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,
|
IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,
|
||||||
IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM
|
IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM,IM
|
||||||
);
|
);
|
||||||
Base64_CHAR_SET = ['A'..'Z','a'..'z','0'..'9','+','/'];
|
//Base64_CHAR_SET = ['A'..'Z','a'..'z','0'..'9','+','/'];
|
||||||
|
|
||||||
function Base64Encode(const ALength : PtrInt; const AInBuffer) : string;
|
function Base64Encode(const ALength : PtrInt; const AInBuffer) : string;
|
||||||
var
|
var
|
||||||
@ -139,7 +139,7 @@ var
|
|||||||
locInQuantom : array[0..3] of Byte;
|
locInQuantom : array[0..3] of Byte;
|
||||||
ok : Boolean;
|
ok : Boolean;
|
||||||
locAtualLen : PtrInt;
|
locAtualLen : PtrInt;
|
||||||
locInValue : Byte;
|
locInValue, locReadedValidChars : Byte;
|
||||||
locFailOnIllegalChar : Boolean;
|
locFailOnIllegalChar : Boolean;
|
||||||
begin
|
begin
|
||||||
if ( AInBuffer = '' ) then begin
|
if ( AInBuffer = '' ) then begin
|
||||||
@ -153,6 +153,7 @@ begin
|
|||||||
locBuffer := @(AInBuffer[1]);
|
locBuffer := @(AInBuffer[1]);
|
||||||
locFailOnIllegalChar := not ( xoDecodeIgnoreIllegalChar in AOptions );
|
locFailOnIllegalChar := not ( xoDecodeIgnoreIllegalChar in AOptions );
|
||||||
while ( locInIndex < locInLen ) do begin
|
while ( locInIndex < locInLen ) do begin
|
||||||
|
locReadedValidChars := 0;
|
||||||
for i := 0 to 3 do begin
|
for i := 0 to 3 do begin
|
||||||
ok := False;
|
ok := False;
|
||||||
while ( locInIndex <= locInLen ) do begin
|
while ( locInIndex <= locInLen ) do begin
|
||||||
@ -172,21 +173,24 @@ begin
|
|||||||
Inc(locPadded);
|
Inc(locPadded);
|
||||||
end;
|
end;
|
||||||
ok := True;
|
ok := True;
|
||||||
|
Inc(locReadedValidChars);
|
||||||
Break;
|
Break;
|
||||||
end else begin
|
end else begin
|
||||||
if locFailOnIllegalChar then
|
if locFailOnIllegalChar then
|
||||||
raise EBase64Exception.Create(s_InvalidEncodedData);
|
raise EBase64Exception.Create(s_InvalidEncodedData);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
if not ok then
|
if ( not ok ) and locFailOnIllegalChar then
|
||||||
raise EBase64Exception.CreateFmt(s_IllegalChar,[Char(locBuffer^)]);
|
raise EBase64Exception.CreateFmt(s_IllegalChar,[Char(locBuffer^)]);
|
||||||
end;
|
end;
|
||||||
|
if ( locReadedValidChars > 0 ) then begin
|
||||||
locOutQuantom[0] := ( locInQuantom[0] shl 2 ) or ( locInQuantom[1] shr 4 );
|
locOutQuantom[0] := ( locInQuantom[0] shl 2 ) or ( locInQuantom[1] shr 4 );
|
||||||
locOutQuantom[1] := ( locInQuantom[1] shl 4 ) or ( locInQuantom[2] shr 2 );
|
locOutQuantom[1] := ( locInQuantom[1] shl 4 ) or ( locInQuantom[2] shr 2 );
|
||||||
locOutQuantom[2] := ( locInQuantom[2] shl 6 ) or ( locInQuantom[3] );
|
locOutQuantom[2] := ( locInQuantom[2] shl 6 ) or ( locInQuantom[3] );
|
||||||
Move(locOutQuantom[0],Result[locAtualLen],3 - locPadded);
|
Move(locOutQuantom[0],Result[locAtualLen],3 - locPadded);
|
||||||
Inc(locAtualLen,3 - locPadded);
|
Inc(locAtualLen,3 - locPadded);
|
||||||
end;
|
end;
|
||||||
|
end;
|
||||||
SetLength(Result,locAtualLen);
|
SetLength(Result,locAtualLen);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
@ -25,6 +25,8 @@ uses
|
|||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
|
{ TTest_Base64 }
|
||||||
|
|
||||||
TTest_Base64 = class(TWstBaseTest)
|
TTest_Base64 = class(TWstBaseTest)
|
||||||
protected
|
protected
|
||||||
procedure Check_Encode(const AIn, AExpect : string);
|
procedure Check_Encode(const AIn, AExpect : string);
|
||||||
@ -45,6 +47,7 @@ type
|
|||||||
procedure Decode_fooba();
|
procedure Decode_fooba();
|
||||||
procedure Decode_foobar();
|
procedure Decode_foobar();
|
||||||
procedure Decode_illegal_char();
|
procedure Decode_illegal_char();
|
||||||
|
procedure Decode_empty();
|
||||||
end;
|
end;
|
||||||
|
|
||||||
TTest_Base16 = class(TWstBaseTest)
|
TTest_Base16 = class(TWstBaseTest)
|
||||||
@ -133,6 +136,28 @@ begin
|
|||||||
CheckEquals(True,ok);
|
CheckEquals(True,ok);
|
||||||
|
|
||||||
Check_Decode('Zm9'#200'vY' + sLineBreak + 'm'#0'Fy','foobar',[xoDecodeIgnoreIllegalChar]);
|
Check_Decode('Zm9'#200'vY' + sLineBreak + 'm'#0'Fy','foobar',[xoDecodeIgnoreIllegalChar]);
|
||||||
|
Check_Decode('Zm9'#200'vY' + sLineBreak + 'm'#0'Fy' + sLineBreak,'foobar',[xoDecodeIgnoreIllegalChar]);
|
||||||
|
Check_Decode('Zm9'#200'vY' + sLineBreak + 'm'#0'Fy' + sLineBreak + sLineBreak,'foobar',[xoDecodeIgnoreIllegalChar]);
|
||||||
|
end;
|
||||||
|
|
||||||
|
procedure TTest_Base64.Decode_empty();
|
||||||
|
var
|
||||||
|
ok : Boolean;
|
||||||
|
begin
|
||||||
|
ok := False;
|
||||||
|
try
|
||||||
|
Check_Decode(sLineBreak,'',[]);
|
||||||
|
except
|
||||||
|
on e : EBase64Exception do
|
||||||
|
ok := True;
|
||||||
|
end;
|
||||||
|
CheckEquals(True,ok);
|
||||||
|
|
||||||
|
Check_Decode('','',[]);
|
||||||
|
Check_Decode(#0,'',[xoDecodeIgnoreIllegalChar]);
|
||||||
|
Check_Decode(sLineBreak,'',[xoDecodeIgnoreIllegalChar]);
|
||||||
|
Check_Decode(sLineBreak + sLineBreak,'',[xoDecodeIgnoreIllegalChar]);
|
||||||
|
Check_Decode(sLineBreak + sLineBreak + sLineBreak,'',[xoDecodeIgnoreIllegalChar]);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TTest_Base64.Encode_empty();
|
procedure TTest_Base64.Encode_empty();
|
||||||
|
Reference in New Issue
Block a user