Page 6 of 9
Re: PureBasic 5.50 beta 1 is out
Posted: Wed Jun 22, 2016 7:21 pm
by Keya
the 5.42 docs for Base64Decoder uses #PB_Ascii|#PB_String_NoZero, but it would also be good if it mentioned somewhere that the buffer needs to be Ascii, if indeed that's still the case! btw I think from the new 5.50 on you can use Ascii() instead of PokeS(etc,#PB_Ascii)

[edit] oops, i meant binary data not ascii string!
Re: PureBasic 5.50 beta 1 is out
Posted: Wed Jun 22, 2016 7:35 pm
by infratec
But then I miss the flag #PB_String_NoZero for the Ascii() procedure.
Or I always have to remember that I have to use MemorySize(*encoded) - 1
Also for UTF8()

Re: PureBasic 5.50 beta 1 is out
Posted: Thu Jun 23, 2016 6:52 am
by Fred
You should use MemoryStringLength() instead of MemorySize(), as ascii strings needs to be zero terminated.
Re: PureBasic 5.50 beta 1 is out
Posted: Thu Jun 23, 2016 8:22 am
by infratec
Fred wrote:You should use MemoryStringLength() instead of MemorySize(), as ascii strings needs to be zero terminated.
Since Base64 don't want the terminating character, I always use AllocateMemory(StringByteLength(..., #PB_Ascii)) which is without terminating character and PokeS(... #PB_String_NoZero)
Then, I think, my MemorySize() is Ok.
Re: PureBasic 5.50 beta 1 is out
Posted: Thu Jun 23, 2016 6:32 pm
by supercdfr
infratec wrote:
Code: Select all
Encoded$ = "aGVsbG8gd29ybGQ="
*encodebuffer = AllocateMemory(StringByteLength(Encoded$))
If *encodebuffer
PokeS(*encodebuffer, Encoded$, -1, #PB_Ascii|#PB_String_NoZero)
*outbuffer = AllocateMemory(MemorySize(*encodebuffer) * 0.8)
If *outbuffer
Length = Base64Decoder(*encodebuffer, MemorySize(*encodebuffer), *outbuffer, MemorySize(*outbuffer))
Debug PeekS(*outbuffer, Length, #PB_Ascii)
FreeMemory(*outbuffer)
EndIf
FreeMemory(*encodebuffer)
EndIf
Bernd
Like someone say, purebasic is not anymore BASIC

.
Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 6:43 am
by infratec
supercdfr wrote:Like someone say, purebasic is not anymore BASIC

.
No, some things are not easy.
But I don't know any language where the only command is:

Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 6:47 am
by Keya
easy? no no, this is Programming! Poetry is two doors down

Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 7:08 am
by Fred
The Base64 commands will be probably be changed somewhen to work on string instead of buffer, as it doesn't makes much sens.
Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 7:24 am
by Keya
encode only strings? but then we wouldnt be able to encode a binary buffer such as an image? email attachments for example
Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 7:35 am
by Little John
The purpose of Base64 is to encode any binary data, not only strings.
Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 7:38 am
by infratec
I think Fred mean what I wrote:
for Base64Decoder() it makes sense that the input is a string.
Be more exactly: an ASCII string.
The output still needs to be a memory buffer, because it can be binary data or a string
Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 7:59 am
by Fred
Yes, that's it. It would be something like that:
Code: Select all
Result = Base64Decoder(Input$, *OutputBuffer, OutputSize)
Result$ = Base64Encoder(*InputBuffer, InputSize [, Flags])
Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 9:15 am
by ts-soft
That's good Fred and make sense!
Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 9:34 am
by Keya
Im just wondering about a situation where for example i read a full .EML file with its base64-encoded attachments into an AllocateMemory(Lof(hFile)) buffer, and then use wilberts FindData module to quickly locate the position of each attachment in the buffer. If Base64Decoder expects a string I would need to make a str=PeekS copy from the buffer? wouldnt be good with large attachments (slower + duplicated memory), especially with base64 sizes being +33% ?
Re: PureBasic 5.50 beta 1 is out
Posted: Fri Jun 24, 2016 9:53 am
by wilbert
Keya wrote:Im just wondering about a situation where for example i read a full .EML file with its base64-encoded attachments into an AllocateMemory(Lof(hFile)) buffer, and then use wilberts FindData module to quickly locate the position of each attachment in the buffer. If Base64Decoder expects a string I would need to make a str=PeekS copy from the buffer? wouldnt be good with large attachments (slower + duplicated memory), especially with base64 sizes being +33% ?
If you need to decode big attachments, you could build you own high speed decoder. The Base64 specification is not that complicated.
For small attachments, the impact on memory and speed probably isn't that big.