Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi 7 - Decode Base64 Using TIdDecoderMIME

OK, well this is driving me nuts, lol.

I have a Base64 string and am trying to decode it into a TMemoryStream using TIdDecoderMIME.

My current code is as follows:

Var MStream:TMemoryStream; Decoder:TIdDecoderMIME;
begin
  Decoder := TIdDecoderMIME.Create(nil);
  MStream := TMemoryStream.Create;
  Decoder.DecodeToStream(BSting,MStream);
end;

Where BString = My Base64 string.

Now when the code is ran, I get an error message saying "Uneven size in DecodeToString."

Any ideas?

Any help is greatly appreciated. Thanks.

like image 996
Josh Line Avatar asked Sep 13 '12 03:09

Josh Line


2 Answers

You're passing to the DecodeToStream function a Base64 string whose length is not a multiple of 4. In other words, the string you're passing is invalid.

like image 200
TLama Avatar answered Nov 10 '22 22:11

TLama


Base64 strings are normally padded with trailing "=" signs to make sure their length is a multiple of 4.

Some decoders will try to correct for the missing padding chars while others will not. See the StackOverflow question "Remove trailing “=” when base64 encoding"

The TIdDecoderMime object validates the input by making sure it is a multiple of 4 - which it will be if the padding chars are included in the input.

like image 45
Mark Elder Avatar answered Nov 10 '22 23:11

Mark Elder