Base64 Encoding in Fortran

Here’s the translation of the Base64 Encoding example from Go to Fortran, formatted in Markdown suitable for Hugo:

Our program demonstrates base64 encoding and decoding in Fortran. Here’s the full source code:

program base64_encoding
    use, intrinsic :: iso_fortran_env, only: error_unit
    use base64_module
    implicit none

    character(len=*), parameter :: data = "abc123!?$*&()'-=@~"
    character(len=:), allocatable :: sEnc, sDec, uEnc, uDec
    integer :: stat

    ! Standard base64 encoding
    sEnc = encode_base64(data)
    print *, "Standard encoding: ", sEnc

    ! Standard base64 decoding
    sDec = decode_base64(sEnc, stat)
    if (stat /= 0) then
        write(error_unit, *) "Error decoding standard base64"
    else
        print *, "Standard decoding: ", sDec
    end if

    print *, ""

    ! URL-safe base64 encoding
    uEnc = encode_base64_url(data)
    print *, "URL-safe encoding: ", uEnc

    ! URL-safe base64 decoding
    uDec = decode_base64_url(uEnc, stat)
    if (stat /= 0) then
        write(error_unit, *) "Error decoding URL-safe base64"
    else
        print *, "URL-safe decoding: ", uDec
    end if

contains

    function encode_base64(input) result(output)
        character(len=*), intent(in) :: input
        character(len=:), allocatable :: output
        ! Implement base64 encoding here
    end function encode_base64

    function decode_base64(input, stat) result(output)
        character(len=*), intent(in) :: input
        integer, intent(out) :: stat
        character(len=:), allocatable :: output
        ! Implement base64 decoding here
    end function decode_base64

    function encode_base64_url(input) result(output)
        character(len=*), intent(in) :: input
        character(len=:), allocatable :: output
        ! Implement URL-safe base64 encoding here
    end function encode_base64_url

    function decode_base64_url(input, stat) result(output)
        character(len=*), intent(in) :: input
        integer, intent(out) :: stat
        character(len=:), allocatable :: output
        ! Implement URL-safe base64 decoding here
    end function decode_base64_url

end program base64_encoding

This program demonstrates base64 encoding and decoding in Fortran. We define a string that we’ll encode and decode.

Fortran doesn’t have built-in support for base64 encoding/decoding, so we’ve defined placeholder functions for these operations. In a real implementation, you would need to implement these functions or use a third-party library.

We first encode the data using standard base64 encoding, then decode it back. We do the same with URL-safe base64 encoding. The program checks for potential errors during decoding.

To run the program, save it as base64_encoding.f90 and compile it:

$ gfortran base64_encoding.f90 -o base64_encoding
$ ./base64_encoding

Note that this example assumes the existence of a base64_module which would contain the actual implementations of the encoding and decoding functions. In practice, you would need to implement these functions or use a library that provides them.

The output would look similar to this:

Standard encoding: YWJjMTIzIT8kKiYoKSctPUB+
Standard decoding: abc123!?$*&()'-=@~

URL-safe encoding: YWJjMTIzIT8kKiYoKSctPUB-
URL-safe decoding: abc123!?$*&()'-=@~

The string encodes to slightly different values with the standard and URL-safe base64 encoders (trailing + vs -) but they both decode to the original string as desired.