Angel Montes de Oca
Angel Montes de Oca

Reputation: 1130

Base64 to bitmap in Inno Setup Pascal Script?

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?

Upvotes: 1

Views: 441

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202118

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;
  Res: Integer;
begin
  Stream := TStringStream.Create('');
  try
    Size := Length(S);
    SetLength(Buffer, Size + 1);
    Res := CryptStringToBinary(S, Size, CRYPT_STRING_BASE64, Buffer, Size, 0, 0);
    if Res = 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.

Upvotes: 3

Related Questions