Base64 encode decode memory and strings

Share your advanced knowledge/code with the community.
User avatar
SinisterSoft
Posts: 77
Joined: Sun Apr 06, 2014 11:41 pm
Location: Preston, UK
Contact:

Base64 encode decode memory and strings

Post by SinisterSoft »

Here are a few routines to help encode decode to/from base64...

Code: Select all

Global base64$="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="

Procedure.l	Base64Decoder(*in,insize,*out,outsize)
	actual=0	
	If insize%4=0
		For loop=0 To insize Step 4
			c0=(FindString(base64$,PeekS(*in,loop,1,#PB_Ascii))-1)&$3f
			c1=(FindString(base64$,PeekS(*in,loop+1,1,#PB_Ascii))-1)&$3f
			PokeB(*out,actual,((c0<<2)|(c1>>4))&$ff)
			actual+1
			If actual>=outsize
				Break
			EndIf
			c2=(FindString(base64$,PeekS(*in,loop+2,1,#PB_Ascii))-1)&$3f
			PokeB(*out,actual,((c1<<4)|(c2>>2))&$ff)
			actual+1
			If actual>=outsize
				Break
			EndIf
			c3=(FindString(base64$,PeekS(*in,loop+3,1,#PB_Ascii))-1)&$3f
			PokeB(*out,actual,((c2<<6)|c3)&$ff)
			actual+1
			If actual>=outsize
				Break
			EndIf
		Next
	EndIf
	ProcedureReturn actual
EndProcedure

Procedure$ Base64Encoder(*in,insize)
	index=0
	out$=""
	While index<insize
		b0=PeekB(*in,index)&$ff
		b1=PeekB(*in,index+1)&$ff
		b2=PeekB(*in,index+2)&$ff
		index+3
		out$+Mid(base64$,((b0>>2)&$3f)+1,1)
		out$+Mid(base64$,(((b0<<4)|(b1>>4))&$3f)+1,1)
		out$+Mid(base64$,(((b1<<2)|(b2>>6))&$3f)+1,1)
		out$+Mid(base64$,(b2&$3f)+1,1)
	Wend
	outsize=((Len(out$)+3)/4)*4
	out$=LSet(out$,outsize,"=")
	ProcedureReturn out$
EndProcedure

Procedure.l Base64Decode(string$,*out,outsize)
	If string$<>""
		insize=((Len(string$)+3)/4)*4
		string$=LSet(string$,insize,"=")
		*input=AllocateMemory(insize+10)
		If *input
			PokeS(*input,0,string$,insize,#PB_Ascii)
			actual=Base64Decoder(*input,insize,*out,outsize)
			FreeMemory(*input)
		EndIf
	EndIf
	ProcedureReturn actual
EndProcedure

Procedure$ Base64DecodeString(string$)
	string$=Trim(string$)
	If string$<>""
		outsize=(Len(string$)*1.5)+64
		*out=AllocateMemory(outsize)
		If *out
			actual=Base64Decode(string$,*out,outsize)
			If actual
				result$=PeekS(*out,0,actual,#PB_UTF8)
			EndIf
			FreeMemory(*out)
		EndIf
	EndIf
	ProcedureReturn result$
EndProcedure

Procedure$ Base64EncodeString(string$)
	If string$<>""
		size=Len(string$)*2
		*mem=AllocateMemory(size)
		If *mem
			size=PokeS(*mem,0,string$,-1,#PB_UTF8)
			result$=Base64Encoder(*mem,size)
			FreeMemory(*mem)
		EndIf
	EndIf
	ProcedureReturn result$
EndProcedure

a$=Base64EncodeString("This is a test")
Debug("encoded="+a$)
Debug("decoded="+Base64DecodeString(a$))
spidernet
Posts: 72
Joined: Tue Feb 02, 2016 3:50 pm

Re: Base64 encode decode memory and strings

Post by spidernet »

Thank you, works well.

Can use with any character set by replacing #PB_UTF8 to #PB_Unicode.
User avatar
SinisterSoft
Posts: 77
Joined: Sun Apr 06, 2014 11:41 pm
Location: Preston, UK
Contact:

Re: Base64 encode decode memory and strings

Post by SinisterSoft »

Maybe, maybe not. You will have to try it to find out. :)
spidernet
Posts: 72
Joined: Tue Feb 02, 2016 3:50 pm

Re: Base64 encode decode memory and strings

Post by spidernet »

Yes, I confirm that it encodes and decodes Asian and Arabic characters if use #PB_Unicode flag. :)
User avatar
SinisterSoft
Posts: 77
Joined: Sun Apr 06, 2014 11:41 pm
Location: Preston, UK
Contact:

Re: Base64 encode decode memory and strings

Post by SinisterSoft »

:)
User avatar
SparrowhawkMMU
Posts: 281
Joined: Wed Aug 19, 2015 3:02 pm
Location: United Kingdom

Re: Base64 encode decode memory and strings

Post by SparrowhawkMMU »

Nice, thank you!

+1
User avatar
Arbrakaan
Posts: 91
Joined: Mon Feb 24, 2014 10:54 pm
Location: Geneva
Contact:

Re: Base64 encode decode memory and strings

Post by Arbrakaan »

Great ! Thanks.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Base64 encode decode memory and strings

Post by Peter »

here's another one:

Code: Select all

Procedure.s Base64EncodeString(Input.s) 
  !  return btoa(encodeURIComponent(v_input).replace(/%([0-9A-F]{2})/g,
  !    function toSolidBytes(match, p1) {
  !      return String.fromCharCode('0x' + p1);
  !  }));
EndProcedure

Procedure.s Base64DecodeString(Input.s)
  !  return decodeURIComponent(atob(v_input).split('').map(function(c) {
  !    return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  !  }).join(''));
EndProcedure

Define myString.s = "Hello World! <!'> öäü 官話 éèê" + Chr(34)

Debug myString

Define b64.s = Base64EncodeString(myString)

Debug b64

Debug Base64DecodeString(b64)
(taken from: https://developer.mozilla.org/en-US/doc ... d_decoding)

Greetings ... Peter
Stefan
Posts: 160
Joined: Mon Feb 05, 2018 9:44 pm

Re: Base64 encode decode memory and strings

Post by Stefan »

How can I decode and encode pictures with this code?
Can you give an example, please?
Post Reply