base64
ivan-pi opened this issue · 2 comments
Motivation
Base64 is an encoding scheme to translate binary data into a set of 64 characters (the Base64 alphabet). It is often used to transfer binary data over channels which only support text. One notable use case of Base64 is in the VTK XML format.
A Fortran interface might look like:
! for T in {real, integer, logical, complex}
function base64_encode(data) result(str)
type(T), intent(in) :: data(..)
character(len=:), allocatable :: str
end function
function base64_decode(str) result(res)
character(len=*), intent(in) :: str
character(len=:), allocatable :: res ! Treated as a byte-array
end function
The decoding phase would need to be followed by transfer
or internal I/O to recover the particular numerical type. Alternatively one could decode directly to a given type by using a mold
argument to retrieve a particular type or use a generic subroutine interface.
Prior Art
Available in multiple third-party libraries:
Also available in standard libraries of other programming languages:
- MATLAB:
matlab.net.base64encode
,matlab.net.base64decode
- Python:
b64encode
,b64decode
- Julia: Base64
The best third-party libraries use SIMD instructions to make the encoding/decoding fast. Details can be found in the works of Muła and Lemire (https://doi.org/10.1145/3132709, https://doi.org/10.1002/spe.2777).
Additional Information
In principle could also be exposed as a language feature: j3-fortran/fortran_proposals#330
Also encountered a wish on Stack Overflow: https://stackoverflow.com/questions/73452098/fortran-write-base64-string-of-binary-to-file
This would be a nice contribution! Just as it might be to add a facility for UTF-8 encoding/decoding with a wrapper to make it OS agnostic.
Looking at the references, it looks like a good starting point would be getting inspiration from the BeFoR64.
Another example is in
https://github.com/urbanjost/M_strings
which includes ENCODE_BASE64() and DECODE_BASE64() functions as well. The TRANSER() function can be used to transer the bytes to types other than text, or also see
https://github.com/urbanjost/M_anything
and the functions ANYTHING_TO_BYTES() and BYTES_TO_ANYTHING() which can be used to allow other types for input and output
.