Page 1 of 1

[Done] Change format of base64-procedures

Posted: Tue Jan 17, 2012 10:02 pm
by HeX0R
While a base64encoder-procedure will allways result in a string, i would prefer this syntax:

Code: Select all

Procedure.s Base64Encoder(*InputBuffer, InputBufferLength)
For the according base64decoder-procedure (returning Length in Bytes):

Code: Select all

Procedure Base64Decoder(Input.s, *OutputBuffer, OutputBufferLength)
And then (hopefully) this annoying unicode-problem is also gone.
(Just had a bug-hunting session, when changing my project to unicode and strange things happened)

Re: Change format of base64-procedures

Posted: Thu Nov 05, 2015 1:15 pm
by ts-soft
+1

Re: Change format of base64-procedures

Posted: Sat Jun 11, 2016 4:38 pm
by infratec
VoilĂ :

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


Procedure.s _Base64Encoder(*InputBuffer, InputBufferLength.i, Flags.i=0)
  
  Protected Result$, OutputBufferLength.i, *OutputBuffer, ResultLength.i
  
  OutputBufferLength = InputBufferLength * 1.35
  If OutputBufferLength < 64
    OutputBufferLength = 64
  EndIf
  
  *OutputBuffer = AllocateMemory(OutputBufferLength)
  If *OutputBuffer
    
    ResultLength = Base64Encoder(*InputBuffer, InputBufferLength, *OutputBuffer, OutputBufferLength, Flags)
    If ResultLength > 0
      Result$ = PeekS(*OutputBuffer, ResultLength, #PB_Ascii)
    EndIf
    FreeMemory(*OutputBuffer)
  EndIf
  
  ProcedureReturn Result$
  
EndProcedure


Procedure.i _Base64Decoder(Input$, *OutputBuffer, OutputBufferLength)
  
  Protected *InputBuffer, Length.i
  
  *InputBuffer = AllocateMemory(StringByteLength(Input$, #PB_Ascii))
  If *InputBuffer
    PokeS(*InputBuffer, Input$, -1, #PB_Ascii|#PB_String_NoZero)
    Length = Base64Decoder(*InputBuffer, MemorySize(*InputBuffer), *OutputBuffer, OutputBufferLength)
    FreeMemory(*InputBuffer)
  EndIf
  
  ProcedureReturn Length
  
EndProcedure


CompilerIf #PB_Compiler_IsMainFile
  
  Define Test$, Base64$, *OutBuffer, Length.i, OutLength.i
  
  Test$ = "Hallo"
  Base64$ = _Base64Encoder(@Test$, StringByteLength(Test$))
  
  Debug Base64$
  
  OutLength = StringByteLength(Base64$, #PB_Ascii) * 0.7
  If OutLength < 64
    OutLength = 64
  EndIf
  *OutBuffer = AllocateMemory(OutLength)
  If *OutBuffer
    
    Length = _Base64Decoder(Base64$, *OutBuffer, MemorySize(*OutBuffer))
    If Length > 0
      ShowMemoryViewer(*OutBuffer, Length)
    EndIf
    
    FreeMemory(*OutBuffer)
  EndIf
  
CompilerEndIf
But as you can see in the example: it is still tricky.

Bernd

Re: Change format of base64-procedures

Posted: Sat Jun 11, 2016 5:02 pm
by infratec
Maybe a better example to reflect that it is normally used for binary data:

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
 
  Define *Buffer, i.i, Base64$, *OutBuffer, Length.i, OutLength.i
 
  *Buffer = AllocateMemory(10)
  If *Buffer
    For i = 0 To 9
      PokeA(*Buffer + i, i)
    Next i
    Base64$ = _Base64Encoder(*Buffer, MemorySize(*Buffer))
    
    Debug Base64$
    
    OutLength = StringByteLength(Base64$, #PB_Ascii) * 0.7
    If OutLength < 64
      OutLength = 64
    EndIf
    *OutBuffer = AllocateMemory(OutLength)
    If *OutBuffer
      
      Length = _Base64Decoder(Base64$, *OutBuffer, MemorySize(*OutBuffer))
      For i = 0 To Length - 1
        Debug PeekA(*OutBuffer + i)
      Next i
      
      FreeMemory(*OutBuffer)
    EndIf
    FreeMemory(*Buffer)
  EndIf
 
CompilerEndIf
Bernd

Re: Change format of base64-procedures

Posted: Sat Jun 11, 2016 7:25 pm
by HeX0R
We are not in the Coding Questions forum here, of course I am able to create those procedures on my own...

Re: Change format of base64-procedures

Posted: Sat Jun 11, 2016 7:36 pm
by Blue
+1

That would definitely be a welcome improvement, although the ASCII-encoded output would still be there, just hidden under the carpet. But I'll gladly accept having the onus of dealing with the mechanical details taken away from me.

Re: Change format of base64-procedures

Posted: Sat Jun 11, 2016 8:12 pm
by infratec
HeX0R wrote:We are not in the Coding Questions forum here, of course I am able to create those procedures on my own...
You can, others not. :wink:

So if this feature request is never accepted, the others have a solution.

But I think the problem is deeper:
Many people does not understand that Base64 is used for transforming binary data and not for text in different codings.
Which is also implied by the examples in the help where always a string is used as data.