Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

base64 #916

Open
ivan-pi opened this issue Jan 6, 2025 · 2 comments
Open

base64 #916

ivan-pi opened this issue Jan 6, 2025 · 2 comments
Labels
idea Proposition of an idea and opening an issue to discuss it

Comments

@ivan-pi
Copy link
Member

ivan-pi commented Jan 6, 2025

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:

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

@ivan-pi ivan-pi added the idea Proposition of an idea and opening an issue to discuss it label Jan 6, 2025
@jalvesz
Copy link
Contributor

jalvesz commented Jan 7, 2025

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.

@lockstockandbarrel
Copy link

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
.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
idea Proposition of an idea and opening an issue to discuss it
Projects
None yet
Development

No branches or pull requests

3 participants