Expand description
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.
- Decode
Err - Error returned from
decode_bufferanddecode.
Constants§
- MAX_
ENCODE_ INPUT_ LEN - The maximum value
encode_len_checkedwill accept before returningNone.
Functions§
- decode
alloc - Similar to
decode_bufferexcept aVecis returned instead using its buffer to write to. - decode_
buffer - Decodes
inputintooutputreturning the subset ofoutputcontaining the decoded data. - decode_
buffer_ exact - Same as
decode_bufferexceptoutputmust have the exact length needed, and the decodedsliceis not returned. - decode_
len - Returns the exact number of bytes needed to decode a base64url without padding input of length
input_length. - encode
alloc - Same as
try_encodeexcept apanicoccurs on allocation failure. - encode_
append alloc - Same as
try_encode_appendexcept apanicoccurs on allocation failure. - encode_
append_ only alloc - Same as
encode_appendexcept the encodedstris not returned. - encode_
buffer - Same as
encode_buffer_checkedexcept apanicoccurs instead ofNonebeing returned. - encode_
buffer_ checked - Encodes
inputintooutputre-interpreting the encoded subset ofoutputas astrbefore returning it. - encode_
buffer_ exact - Same as
encode_bufferexceptoutputmust have the exact length needed to encodeinput, and the encodedstris not returned. - encode_
len - Same as
encode_len_checkedexcept apanicoccurs instead ofNonebeing 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
inputusingwriter. - try_
encode alloc - Similar to
encode_bufferexcept aStringis returned instead using its buffer to write to. - try_
encode_ append alloc - Appends the base64url encoding of
inputtosreturning thestrthat was appended. - try_
encode_ append_ only alloc - Same as
try_encode_appendexcept the encodedstris not returned. - validate_
encoded_ data - Similar to
decode_bufferexcept the data is not decoded.