Skip to main content

Crate base64url_nopad

Crate base64url_nopad 

Source
Expand description

git crates-io docs-rs

base64url_nopad is a library for fast, efficient, correct, and const encoding and decoding of base64url without padding data. All functions that can be const are const. Great care is made to ensure all arithmetic is free from “side effects” (e.g., overflow). panics are avoided at all costs unless explicitly documented including panics related to memory allocations.

§base64url_nopad in action

/// Length of our input to encode.
const INPUT_LEN: usize = 259;
/// The base64url encoded value without padding of our input.
const ENCODED_VAL: &str = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_1MH0Q";
let mut input = [0; INPUT_LEN];
for i in 0..=255 {
    input[usize::from(i)] = i;
}
input[256] = 83;
input[257] = 7;
input[258] = 209;
let mut output = [0; base64url_nopad::encode_len(INPUT_LEN)];
assert_eq!(base64url_nopad::encode_buffer(&input, &mut output), ENCODED_VAL);
assert_eq!(base64url_nopad::decode_len(output.len()), Some(INPUT_LEN));
base64url_nopad::decode_buffer(ENCODED_VAL.as_bytes(), &mut output[..INPUT_LEN])?;
assert_eq!(input, output[..INPUT_LEN]);

§Cargo “features”

§alloc

Enables support for memory allocations via alloc.

§Correctness of code

This library is written in a way that is free from any overflow, underflow, or other kinds of “arithmetic side effects”. All functions that can panic are explicitly documented as such; and all possible panics are isolated to convenience functions that panic instead of error. Strict encoding and decoding is performed; thus if an input contains any invalid data, it is guaranteed to fail when decoding it (e.g., trailing non-zero bits).

Enums§

Alphabet
The base64url alphabet.
DecodeErr
Error returned from decode_buffer and decode.

Constants§

MAX_ENCODE_INPUT_LEN
The maximum value encode_len_checked will accept before returning None.

Functions§

decodealloc
Similar to decode_buffer except a Vec is returned instead using its buffer to write to.
decode_buffer
Decodes input into output returning the subset of output containing the decoded data.
decode_buffer_exact
Same as decode_buffer except output must have the exact length needed, and the decoded slice is not returned.
decode_len
Returns the exact number of bytes needed to decode a base64url without padding input of length input_length.
encodealloc
Same as try_encode except a panic occurs on allocation failure.
encode_appendalloc
Same as try_encode_append except a panic occurs on allocation failure.
encode_append_onlyalloc
Same as encode_append except the encoded str is not returned.
encode_buffer
Same as encode_buffer_checked except a panic occurs instead of None being returned.
encode_buffer_checked
Encodes input into output re-interpreting the encoded subset of output as a str before returning it.
encode_buffer_exact
Same as encode_buffer except output must have the exact length needed to encode input, and the encoded str is not returned.
encode_len
Same as encode_len_checked except a panic occurs instead of None being returned.
encode_len_checked
Returns the exact number of bytes needed to encode an input of length input_length.
encode_write
Writes the base64url encoding of input using writer.
try_encodealloc
Similar to encode_buffer except a String is returned instead using its buffer to write to.
try_encode_appendalloc
Appends the base64url encoding of input to s returning the str that was appended.
try_encode_append_onlyalloc
Same as try_encode_append except the encoded str is not returned.
validate_encoded_data
Similar to decode_buffer except the data is not decoded.