[Done] Change format of base64-procedures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

[Done] Change format of base64-procedures

Post 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)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Change format of base64-procedures

Post by ts-soft »

+1
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Change format of base64-procedures

Post 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
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Change format of base64-procedures

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Change format of base64-procedures

Post by HeX0R »

We are not in the Coding Questions forum here, of course I am able to create those procedures on my own...
User avatar
Blue
Addict
Addict
Posts: 964
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Change format of base64-procedures

Post 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.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Change format of base64-procedures

Post 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.
Post Reply