-
Notifications
You must be signed in to change notification settings - Fork 225
feat: add base64 support #1171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RatanKokal
wants to merge
9
commits into
fortran-lang:master
Choose a base branch
from
RatanKokal:wip_base64
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: add base64 support #1171
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3d575e2
feat: add base64 support
RatanKokal 3cb5be3
perf: optimize base64 decoder with O(1) LUT and single-pass bit accum…
RatanKokal d6d3103
refactor(strings): harden Base64 scalar fallback and align APIs
RatanKokal 70e3540
string: improve base64 standard compliance and performance
RatanKokal e21a325
ref: base64 architecture to submodules and apply stdlib standards
RatanKokal 6a013bf
fix(base64): address review feedback for portability and style
RatanKokal 4b9ac9b
docs(base64): add stdlib_base64 spec
RatanKokal 94b8def
refactor(strings): address reviewer feedback for base64 implementation
RatanKokal 6c0f797
refactor(base64): Use enc_chunk and raw_chunk
RatanKokal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| --- | ||
| title: base64 | ||
| --- | ||
|
|
||
| # The `stdlib_base64` module | ||
|
|
||
| [TOC] | ||
|
|
||
| ## Introduction | ||
|
|
||
| The `stdlib_base64` module provides procedures to encode and decode intrinsic | ||
| Fortran data using the Base64 encoding scheme defined by RFC 4648. | ||
|
|
||
| ## Specification of the `stdlib_base64` procedures | ||
|
|
||
| ### `base64_encode` | ||
|
|
||
| #### Status | ||
|
|
||
| Experimental | ||
|
|
||
| #### Description | ||
|
|
||
| Encodes intrinsic arrays into Base64 text. | ||
|
|
||
| This is the ergonomic API for encoding. It allocates and returns the output | ||
| string automatically. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `res =` [[stdlib_base64(module):base64_encode(interface)]] `(data)` | ||
|
|
||
| #### Class | ||
|
|
||
| Function. | ||
|
|
||
| #### Argument | ||
|
|
||
| `data`: shall be a contiguous array of an intrinsic type (`real`, `integer`, | ||
| `complex`, `logical`, or `integer(int8)`). It is an `intent(in)` argument. | ||
|
|
||
| #### Result value | ||
|
|
||
| The result `res` is an allocatable character string (`character(len=:)`) | ||
| containing the Base64 representation of the input `data`. | ||
|
|
||
| #### Example | ||
|
|
||
| ```fortran | ||
| {!example/strings/example_base64_encode.f90!} | ||
| ``` | ||
|
|
||
| ### `base64_encode_into` | ||
|
|
||
| #### Status | ||
|
|
||
| Experimental | ||
|
|
||
| #### Description | ||
|
|
||
| Encodes bytes into a caller-provided output buffer. | ||
|
|
||
| This is the preallocated API for throughput-sensitive workflows. It does not | ||
| allocate and reports status through `err_state`. On success, | ||
| `err_state%ok()` is `.true.` and `encoded_len` is the number of meaningful | ||
| characters written to `str`. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `call` [[stdlib_base64(module):base64_encode_into(subroutine)]] & | ||
| `(bytes, str, encoded_len, err_state)` | ||
|
|
||
| #### Class | ||
|
|
||
| Pure module subroutine. | ||
|
|
||
| #### Arguments | ||
|
|
||
| `bytes`: shall be a contiguous array of type `integer(int8)`. It is an | ||
| `intent(in)` argument. | ||
|
|
||
| `str`: shall be an intrinsic character type. It is an `intent(out)` argument. | ||
|
|
||
| `encoded_len`: shall be an `integer`. It is an `intent(out)` argument | ||
| representing the number of encoded characters written. | ||
|
|
||
| `err_state`: shall be a `type(state_type)` from `stdlib_error`. It is an | ||
| `intent(out)` argument used to report success or errors. | ||
|
|
||
| #### Example | ||
|
|
||
| ```fortran | ||
| {!example/strings/example_base64_encode_into.f90!} | ||
| ``` | ||
|
|
||
| ### `base64_decode` | ||
|
|
||
| #### Status | ||
|
|
||
| Experimental | ||
|
|
||
| #### Description | ||
|
|
||
| Decodes Base64 text and returns an allocated byte string. | ||
|
|
||
| This is the ergonomic API for decoding. It allocates and returns the result | ||
| automatically. On error, an empty result is returned. If `err_state` is | ||
| present, error details are reported there. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `res =` [[stdlib_base64(module):base64_decode(function)]] `(str [, err_state])` | ||
|
|
||
| #### Class | ||
|
|
||
| Function. | ||
|
|
||
| #### Arguments | ||
|
|
||
| `str`: shall be an intrinsic character type. It is an `intent(in)` argument. | ||
|
|
||
| `err_state` (optional): shall be a `type(state_type)` from `stdlib_error`. | ||
| It is an `intent(out)` argument used to report invalid Base64 sequences. | ||
|
|
||
| #### Result value | ||
|
|
||
| The result `res` is an allocatable character string (`character(len=:)`) | ||
| containing the decoded bytes. | ||
|
|
||
| #### Example | ||
|
|
||
| ```fortran | ||
| {!example/strings/example_base64_decode.f90!} | ||
| ``` | ||
|
|
||
| ### `base64_decode_into` | ||
|
|
||
| #### Status | ||
|
|
||
| Experimental | ||
|
|
||
| #### Description | ||
|
|
||
| Decodes Base64 text into a caller-provided output buffer. | ||
|
|
||
| This is the preallocated API for throughput-sensitive workflows. It does not | ||
| allocate and reports status through `err_state`. The optional `skip_despace` | ||
| argument can be used when the input is already whitespace-free to bypass the | ||
| despacing step. | ||
|
|
||
| #### Syntax | ||
|
|
||
| `call` [[stdlib_base64(module):base64_decode_into(subroutine)]] & | ||
| `(str, res, decoded_len, err_state [, skip_despace])` | ||
|
|
||
| #### Class | ||
|
|
||
| Pure module subroutine. | ||
|
|
||
| #### Arguments | ||
|
|
||
| `str`: shall be an intrinsic character type. It is an `intent(in)` argument. | ||
|
|
||
| `res`: shall be an intrinsic character type. It is an `intent(out)` argument. | ||
|
|
||
| `decoded_len`: shall be an `integer`. It is an `intent(out)` argument | ||
| representing the number of decoded bytes written. | ||
|
|
||
| `err_state`: shall be a `type(state_type)` from `stdlib_error`. It is an | ||
| `intent(out)` argument used to report success or errors. | ||
|
|
||
| `skip_despace` (optional): shall be a `logical`. It is an `intent(in)` | ||
| argument. If `.true.`, the routine assumes the input contains no whitespace. | ||
|
|
||
| #### Example | ||
|
|
||
| ```fortran | ||
| {!example/strings/example_base64_decode_into.f90!} | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| program example_base64_decode | ||
| use stdlib_base64, only: base64_decode | ||
| implicit none | ||
|
|
||
| character(len=:), allocatable :: decoded | ||
|
|
||
| decoded = base64_decode("TWFu") | ||
|
|
||
| print '(a)', decoded | ||
| end program example_base64_decode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| program example_base64_decode_into | ||
| use stdlib_base64, only: base64_decode_into | ||
| use stdlib_error, only: state_type | ||
| implicit none | ||
|
|
||
| character(len=4) :: str = "TWFu" | ||
| character(len=3) :: res | ||
| integer :: decoded_len | ||
| type(state_type) :: err_state | ||
|
|
||
| call base64_decode_into(str, res, decoded_len, err_state) | ||
|
|
||
| if (err_state%ok()) then | ||
| print '(a)', res | ||
| else | ||
| print '(a)', "Decoding failed" | ||
| end if | ||
| end program example_base64_decode_into |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| program example_base64_encode | ||
| use stdlib_base64, only: base64_encode | ||
| use stdlib_kinds, only: int8 | ||
| implicit none | ||
|
|
||
| character(len=:), allocatable :: encoded | ||
| integer(int8) :: bytes(3) | ||
|
|
||
| bytes = [77_int8, 97_int8, 110_int8] | ||
| encoded = base64_encode(bytes) | ||
|
|
||
| print '(a)', encoded | ||
| end program example_base64_encode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| program example_base64_encode_into | ||
| use stdlib_base64, only: base64_encode_into | ||
| use stdlib_kinds, only: int8 | ||
| use stdlib_error, only: state_type | ||
| implicit none | ||
|
|
||
| integer(int8) :: bytes(3) | ||
| character(len=4) :: str | ||
| integer :: encoded_len | ||
| type(state_type) :: err_state | ||
|
|
||
| bytes = [77_int8, 97_int8, 110_int8] | ||
| call base64_encode_into(bytes, str, encoded_len, err_state) | ||
|
|
||
| if (err_state%ok()) then | ||
| print '(a)', str | ||
| else | ||
| print '(a)', "Encoding failed" | ||
| end if | ||
| end program example_base64_encode_into |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.