1
votes

I have a XML with some flags in Base64.

I want to decode them to show them on my installer's list box, is there any way to do it?

1
While you can convert the Base64 to image in Inno Setup, the TListBox does not support displaying images. - Martin Prikryl

1 Answers

2
votes

To convert a Base64 string to actual binary data, you can use the CryptStringToBinary Windows API function.

function CryptStringToBinary(
  sz: string; cch: LongWord; flags: LongWord; binary: string; var size: LongWord;
  skip: LongWord; flagsused: LongWord): Integer;
  external '[email protected] stdcall';

const
  CRYPT_STRING_BASE64 = $01;

procedure LoadBitmapFromBase64(Bitmap: TBitmap; S: string);
var
  Stream: TStream;
  Buffer: string;
  Size: LongWord;
begin
  Stream := TStringStream.Create('');
  try
    SetLength(Buffer, (Length(S) div 2) + 1);
    Size := Length(S);
    if CryptStringToBinary(S, Length(S), CRYPT_STRING_BASE64, Buffer, Size, 0, 0) = 0 then
    begin
      RaiseException('Error decoding Base64 string');
    end;

    Stream.WriteBuffer(Buffer, Size);

    Stream.Position := 0;
    Bitmap.LoadFromStream(Stream);
  finally
    Stream.Free;
  end;
end;

The code requires the Unicode version of Inno Setup (the only version as of Inno Setup 6). You should not use the Ansi version anyway, in the 21st century. Though ironically, implementing this in the Ansi version would be way easier. See my answer to Writing binary file in Inno Setup for a use of the CryptStringToBinary that's compatible with both Ansi and Unicode version of Inno Setup.