ALAppExtensions icon indicating copy to clipboard operation
ALAppExtensions copied to clipboard

"Base64 Convert" is broken

Open navdotnetreqs opened this issue 6 years ago • 9 comments

I use Base64 converter to decode a base64-encoded string. When I encode the result, it differs from the original. Sample code below.

//Base64: codeunit "Base64 Convert"; Base64String := 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAHCAIAAABPxRC5AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAAgSURBVBhXY/iPCkB8BhhA8EFSYECkPIQDBAgWCPz/DwAmK02zB8YDEgAAAABJRU5ErkJggg=='; Message(Base64String); lTxtType := base64.FromBase64(Base64String); message(base64.ToBase64(lTxtType));

The latter message gives us: 77+9UE5HDQoaCgAAAA1JSERSAAAABQAAAAcIAgAAAE/vv70Q77+9AAAAAXNSR0IA77+977+9HO+/vQAAAARnQU1BAADvv73vv70L77+9YQUAAAAJcEhZcwAAEnQAABJ0Ae+/vWYfeAAAACBJREFUGFdj77+977+9CkB8BhhA77+9QVJgQO+/vTzvv70DBAgWCO+/ve+/vQ8AJitN77+9B++/vQMSAAAAAElFTkTvv71CYO+/vQ==

BC 15.2

navdotnetreqs avatar Feb 06 '20 12:02 navdotnetreqs

As a further clarification, we want to write the base64 string as binary data into a MediaSet, but I have not found a way to do this with the very limited toolset (dotnet wrapper codeunits) available to us.

navdotnetreqs avatar Feb 06 '20 13:02 navdotnetreqs

@JesperSchulz

AlexanderYakunin avatar Feb 07 '20 07:02 AlexanderYakunin

Anything ?

navdotnetreqs avatar Feb 14 '20 10:02 navdotnetreqs

I've looked into our Base64 module and it looks as if it's sufficiently tested: https://github.com/microsoft/ALAppExtensions/blob/master/Modules/System%20Tests/Base64%20Convert/src/Base64ConvertTest.Codeunit.al

In that test codeunit, you will also find the following test, which kind of does the same as you are doing:

    [Test]
    procedure ToAndFromBase64Test()
    var
        ConvertedText: Text;
        ConvertedBackText: Text;
    begin
        // [SCENARIO] The convertion to base-64 string and then from it back results in the initial string.

        // [WHEN] The string is converted to base-64
        ConvertedText := Base64Convert.ToBase64(SampleTxt);

        // [WHEN] The string is converted back from base-64
        ConvertedBackText := Base64Convert.FromBase64(ConvertedText);

        // [THEN] The value is the same as the initial one
        Assert.AreEqual(SampleTxt, ConvertedBackText, 'The converted back value is not the same as the initial one.');
    end;

We take a string, Base64 convert it and covert it back, which yields the same result. I would hence claim, that the implementation isn't broken. But we'll look into your specific issue to see what's going on there.

JesperSchulz avatar Feb 18 '20 15:02 JesperSchulz

Good day and thanks for your reply.

Yes, converting to base64 and then back does seem to work, at least for a non-binary string, but converting from 64 to binary and then back does not. If this relates to how text variables work and is impossible to do, we would appreciate help in how to write the binary representation of the base64 string into a mediaset.

navdotnetreqs avatar Feb 19 '20 05:02 navdotnetreqs

@navdotnetreqs can you try using the overloads of ToBase64 and FromBase64 that accep streams (InStream and OutStream respectively)? This are the functions that are supposed to be used for binary content.

IhorHandziuk avatar Feb 19 '20 16:02 IhorHandziuk

Hi and thanks for your reply.

Using FromBase64(Text,OutStream) I managed to solve the problem in question by writing the base64 data to a blob field in a temporary record (company information picture), and then creating an instream to the same blob, and then using mediaset.ImportStream.

Thank you for your help.

navdotnetreqs avatar Feb 19 '20 18:02 navdotnetreqs

I am experiencing the same issue. So the TempBlob table was removed... and to achieve the same functionality as before we need to "abuse" another table with a blob field to mimic the old TempBlob behaviour. This sounds like a bad joke.

StijnBt avatar Sep 17 '20 15:09 StijnBt

Nevermind, actually simple... if you know how:

procedure ImportLabelFromBase64(Base64Text: Text; Description: Text)
var
        TempBlob: Codeunit "Temp Blob";
        Base64Convert: Codeunit "Base64 Convert";
        LabelInStream: InStream;
        BlobOutStream: OutStream;
    begin
        TempBlob.CreateOutStream(BlobOutStream);
        Base64Convert.FromBase64(Base64Text, BlobOutStream);
        TempBlob.CreateInStream(LabelInStream);
        Label.ImportStream(LabelInStream, Description);
    end;

StijnBt avatar Oct 01 '20 10:10 StijnBt