[Implemented] More robust Base64
Posted: Thu Jun 30, 2016 2:05 pm
Sorry for long post and nitpicking!
But if the Base64 library is going to be updated for 5.50, here are some improvements to consider
1. The Encoder has a flag to remove the trailing '=' padding... but the Decoder cannot handle removed padding!
2. The Decoder seems to ignore/exceed the specified OutputSize! (Shouldn't it fail and return 0?)
3. The Encoder also ignores/exceeds the specified OutputSize. (Shouldn't it fail and return 0?)
But if the Base64 library is going to be updated for 5.50, here are some improvements to consider

1. The Encoder has a flag to remove the trailing '=' padding... but the Decoder cannot handle removed padding!
Code: Select all
Encoded$ = "SGVsbG8gV29ybGQ=" ; Base64("Hello World")
*Input = Ascii(Encoded$)
InputSize = StringByteLength(Encoded$, #PB_Ascii)
*Output = AllocateMemory(1000)
Debug Base64Decoder(*Input, InputSize, *Output, 1000)
Debug PeekS(*Output, -1, #PB_Ascii)
Code: Select all
Encoded$ = "SGVsbG8gV29ybGQ" ; Base64("Hello World") without padding
*Input = Ascii(Encoded$)
InputSize = StringByteLength(Encoded$, #PB_Ascii)
*Output = AllocateMemory(1000)
Debug Base64Decoder(*Input, InputSize, *Output, 1000)
Debug PeekS(*Output, -1, #PB_Ascii)
Code: Select all
Encoded$ = "SGVsbG8gV29ybGQ=" ; Base64("Hello World")
*Input = Ascii(Encoded$)
InputSize = StringByteLength(Encoded$, #PB_Ascii)
*Output = AllocateMemory(5) ; buffer too small!
Debug Base64Decoder(*Input, InputSize, *Output, 5)
Debug PeekS(*Output, -1, #PB_Ascii)
FreeMemory(*Output)
FreeMemory(*Input)
Code: Select all
Text$ = "Hello World"
*Input = Ascii(Text$)
InputSize = StringByteLength(Text$, #PB_Ascii)
*Output = AllocateMemory(15) ; buffer too small! should be 17 (incl NUL)
Debug Base64Encoder(*Input, InputSize, *Output, 15)
Debug PeekS(*Output, -1, #PB_Ascii)
FreeMemory(*Output)
FreeMemory(*Input)