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:
inoussa 2009-02-14 01:10:41 +00:00
parent d7d4a207dc
commit 949491a27e
2 changed files with 37 additions and 8 deletions

View File

@ -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
);
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;
var
@ -139,7 +139,7 @@ var
locInQuantom : array[0..3] of Byte;
ok : Boolean;
locAtualLen : PtrInt;
locInValue : Byte;
locInValue, locReadedValidChars : Byte;
locFailOnIllegalChar : Boolean;
begin
if ( AInBuffer = '' ) then begin
@ -153,6 +153,7 @@ begin
locBuffer := @(AInBuffer[1]);
locFailOnIllegalChar := not ( xoDecodeIgnoreIllegalChar in AOptions );
while ( locInIndex < locInLen ) do begin
locReadedValidChars := 0;
for i := 0 to 3 do begin
ok := False;
while ( locInIndex <= locInLen ) do begin
@ -172,20 +173,23 @@ begin
Inc(locPadded);
end;
ok := True;
Inc(locReadedValidChars);
Break;
end else begin
if locFailOnIllegalChar then
raise EBase64Exception.Create(s_InvalidEncodedData);
end;
end;
if not ok then
if ( not ok ) and locFailOnIllegalChar then
raise EBase64Exception.CreateFmt(s_IllegalChar,[Char(locBuffer^)]);
end;
locOutQuantom[0] := ( locInQuantom[0] shl 2 ) or ( locInQuantom[1] shr 4 );
locOutQuantom[1] := ( locInQuantom[1] shl 4 ) or ( locInQuantom[2] shr 2 );
locOutQuantom[2] := ( locInQuantom[2] shl 6 ) or ( locInQuantom[3] );
Move(locOutQuantom[0],Result[locAtualLen],3 - locPadded);
Inc(locAtualLen,3 - locPadded);
if ( locReadedValidChars > 0 ) then begin
locOutQuantom[0] := ( locInQuantom[0] shl 2 ) or ( locInQuantom[1] shr 4 );
locOutQuantom[1] := ( locInQuantom[1] shl 4 ) or ( locInQuantom[2] shr 2 );
locOutQuantom[2] := ( locInQuantom[2] shl 6 ) or ( locInQuantom[3] );
Move(locOutQuantom[0],Result[locAtualLen],3 - locPadded);
Inc(locAtualLen,3 - locPadded);
end;
end;
SetLength(Result,locAtualLen);
end;

View File

@ -25,6 +25,8 @@ uses
type
{ TTest_Base64 }
TTest_Base64 = class(TWstBaseTest)
protected
procedure Check_Encode(const AIn, AExpect : string);
@ -45,6 +47,7 @@ type
procedure Decode_fooba();
procedure Decode_foobar();
procedure Decode_illegal_char();
procedure Decode_empty();
end;
TTest_Base16 = class(TWstBaseTest)
@ -133,6 +136,28 @@ begin
CheckEquals(True,ok);
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;
procedure TTest_Base64.Encode_empty();