Our conversion routines expect pure base-64 characters, and pre-compute the resulting buffer size.
To be fair, our main usage was binary storage - e.g. for Blobs - as JSON string.
For our use case, our routines are very optimized and efficient. And also safer, since they detect any unexpected/invalid char in the input, whereas DecodeBase64() doesn't.
If you want to handle external base-64 content, as you need, just process the input as TrimControlChars().
]]>But I didn't really know about this TrimControlChars function.
But based on what it does the name shouldn't be RemoveControlChars?
]]>I think this replace will be safer if you consider only #13 as a line break as well.
pdf := docs._[i].S['pdf']; pdf := StringReplaceAll(pdf, #13#10, ''); pdf := StringReplaceAll(pdf, #13, '');
NOTE: The order matters. You should replace #13#10 first, and then #13
Thank you too. But I chose Arnaud's tip because it's cleaner.
]]>But, just thinking, if the data that I've provided is considered valid, shouldn't we add a TrimControlChars() call in Base64 functions before conversion?
I've asked the guy and we tested using Java.util (built-in) and Apache commons. Both add line feed in the output...
pdf := TrimControlChars(pdf);
pdf := docs._[i].S['pdf'];
pdf := StringReplaceAll(pdf, #13#10, '');
pdf := StringReplaceAll(pdf, #13, '');
NOTE: The order matters. You should replace #13#10 first, and then #13
]]> pdf := StringReplaceAll(docs._[i].S['pdf'], #13#10, '');
//FileFromString(DecodeBase64(pdf), fn, True);
FileFromString(Base64ToBin(pdf), fn, True);
Could you put the input text into a gist so that we reproduce it?
I couldn't send the private PDF so, I asked the other part to make a fake one.
Here https://gist.github.com/mdbs99/fb6ddf23 … 2fff7a82f7
The structure is:
<Parametros>
<docs>
<doc>
<nome>filename</nome>
<pdf>base64</pdf>
...
</Parametros>
Consider this:
var
i: Integer;
fn: TFileName;
pdf: RawByteString;
begin
{...}
{ docs is a TDocVariantData...}
for i := 0 to docs.Count-1 do
begin
{find the correct PDF selected by user...}
pdf := docs._[i].S['pdf'];
// FileFromString(DecodeBase64(pdf), fn, True); //<< this works
FileFromString(Base64ToBin(pdf), fn, True); // << this does not
ShellExecute(fn); // starts Adobe PDF
end;
end;
- synacode.DecodeBase64() from Synapse reads perfectly
- XML looks like UTF8
- I've tried to make some conversions, like UTF8ToString / StringToUF8, SynUnicodeTo ... etc, but in vain.
Any ideas?
best regards.
]]>