Skip to content

base64 #916

Open
Open
base64#916
@ivan-pi

Description

@ivan-pi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ideaProposition of an idea and opening an issue to discuss it

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions