Recent

Author Topic: DecodeStringBase64 bug?  (Read 2185 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
DecodeStringBase64 bug?
« on: July 31, 2018, 04:08:51 pm »
Hi guys, if you try to launch my example and press the button, do you tell me if the program is wrong? I can only try it on mac os x right now. But I can not understand why it goes wrong.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

bytebites

  • Hero Member
  • *****
  • Posts: 778
Re: DecodeStringBase64 bug?
« Reply #1 on: July 31, 2018, 07:01:12 pm »
It works if you add one letter.

wp

  • Hero Member
  • *****
  • Posts: 13486
Re: DecodeStringBase64 bug?
« Reply #2 on: July 31, 2018, 11:46:58 pm »
wikipedia tells about base64:
Quote
Each base64 digit represents exactly 6 bits of data. Three 8-bit bytes (i.e., a total of 24 bits) can therefore be represented by four 6-bit base64 digits.
[...]
'=' padding characters might be added to make the last encoded block contain four Base64 characters.
Therefore you should pad your Base64 string with '=' characters to have only complete blocks of four Base64 characters:

Code: Pascal  [Select][+][-]
  1. function PadBase64(ABase64String: String): String;
  2. begin
  3.   Result := ABase64String;
  4.   while Length(Result) mod 4 > 0 do Result := Result + '=';
  5. end;
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. begin
  9.      ShowMessage(DecodeStringBase64(PadBase64(Edit1.Text)));
  10. end;

Nevertheless, reading the header of the "base64" unit shows
Quote
  (* The TBase64DecodingStream supports two modes:
   * - 'strict mode':
   *    - follows RFC3548
   *    - rejects any characters outside of base64 alphabet,
   *    - only accepts up to two '=' characters at the end and
   *    - requires the input to have a Size being a multiple of 4; otherwise raises an EBase64DecodingException
   * - 'MIME mode':
   *    - follows RFC2045
   *    - ignores any characters outside of base64 alphabet
   *    - takes any '=' as end of string
   *    - handles apparently truncated input streams gracefully   
Since the function DecodeStringBase64 is called without additional boolean parameter the decoder is in MIME mode, and the last line tells that your original code should not crash. Therefore, I think that this behavior is a bug, and I filed a bug report: https://bugs.freepascal.org/view.php?id=34071

xinyiman

  • Hero Member
  • *****
  • Posts: 2261
    • Lazarus and Free Pascal italian community
Re: DecodeStringBase64 bug?
« Reply #3 on: August 01, 2018, 08:11:18 am »
Thanks wp, I had read that too. In fact, that's why I assumed it was a bug. I solve it by checking that it is a multiple of 4. Actually I tried and I must say that it is enough to be above 3 characters to not go wrong. Good day
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

wp

  • Hero Member
  • *****
  • Posts: 13486
Re: DecodeStringBase64 bug?
« Reply #4 on: August 01, 2018, 11:11:46 am »
I solve it by checking that it is a multiple of 4. Actually I tried and I must say that it is enough to be above 3 characters to not go wrong.
Not crashing does not mean that the result is correct. Please see the demo project which I attached to the bug report mentioned above. The first two test strings are longer than 3 chars, but not a multiple of 4 chars. Compared to the string padded with '=' to a multiple of 4 the decoded string is truncated.

 

TinyPortal © 2005-2018