B4R Code Snippet Base64 en-/decode via inline C

In Globals:

B4X:
Public b64encoded (255) As Byte
Public b64encoded_len As ULong
    
Public b64decoded (255) As Byte
Public b64decoded_len As ULong

Add to your code and call the Base64 sub

B4X:
Sub Base64
    Dim s As String="Just a text xxxxxwgwioegjw49w9gjwjogjwpogjwjgwjgwjvknvvnpggh9ag9hghaoajxx"
    bc.ArrayCopy2(s,0,b64decoded,0,s.Length)
    b64decoded_len=s.Length
    Log("Cleartext: ", b64decoded)
    RunNative("Base64Encode", Null)
    Log("Base64: ",b64encoded)
    Log("Lenght: ",b64encoded_len)
    
    For i = 0 To b64decoded.Length-1
        b64decoded(i)=0
    Next
    b64decoded_len=0
    RunNative("Base64Decode", Null)
    Log("Base64: ",b64decoded)
    Log("Lenght: ",b64decoded_len)

    
    
End Sub

#if c
#include "mbedtls/base64.h"
void Base64Encode (B4R::Object* o) {

mbedtls_base64_encode(
    (unsigned char *)b4r_main::_b64encoded->data,
    b4r_main::_b64encoded->length,
    &b4r_main::_b64encoded_len,
    (unsigned char *)b4r_main::_b64decoded->data,
    b4r_main::_b64decoded_len
    );

}
#End If

#if c
#include "mbedtls/base64.h"
void Base64Decode (B4R::Object* o) {

    mbedtls_base64_decode(

    (unsigned char *)b4r_main::_b64decoded->data,
    b4r_main::_b64decoded->length,
    &b4r_main::_b64decoded_len,
    (unsigned char *)b4r_main::_b64encoded->data,
    b4r_main::_b64encoded_len
    );
}
#End If
 

hatzisn

Expert
Licensed User
Longtime User
It sounds good. I will definitely try it. Is this library you include a built in library in Arduino and if not can you post a link where to download It to avoid searching. Why don't you make it a b4xlib with methods Base64.Encode and Base64.Decode?
 

KMatle

Expert
Licensed User
Longtime User
It sounds good. I will definitely try it. Is this library you include a built in library in Arduino and if not can you post a link where to download It to avoid searching. Why don't you make it a b4xlib with methods Base64.Encode and Base64.Decode?

It's all built in (the mbedtls core is quite mighty). No special libs needed. Unfortunately I never tried to build a c lib. Will take a look at it.

Generally we should have the most important functions as a lib (preventing inline c)
 

hatzisn

Expert
Licensed User
Longtime User
No, you misunderstood me... I said a b4xlib. You just move the same code to a second module named Base64, you change it to a sub accepting an argument, you change slightly the C code (b4r_main::_ .... to b4r_base64::_ ...), you create a manifest (download a B4R b4xlib of the ones I have created from the link 'my contributions to the community' in my signature and open it with winrar to get a grip of how it looks like - b4xlib files are just zip files with renamed extension) and finally zip the Base64.bas file and the manifest and just rename its extension to b4xlib. You can download the rURL.b4xlib (created in cooperation with @Erel) and copy its logic.
 

hatzisn

Expert
Licensed User
Longtime User
Hi my Arduino 1.8.15 does not have installed the mbedtls library. I have searched for it and I have found the following 2 github pages. The second obviously is for ARM based embeded systems right? I do not know how to compile it and I was wondering if you can put it in your Google Drive to download it and install it.


 
Last edited:
Top