Skip to main content

base64_url/
decode.rs

1use alloc::vec::Vec;
2use core::slice::from_raw_parts_mut;
3
4use base64::Engine;
5
6/// Decode a Base64-URL string to data.
7#[inline]
8pub fn decode<T: ?Sized + AsRef<[u8]>>(input: &T) -> Result<Vec<u8>, base64::DecodeError> {
9    base64::engine::general_purpose::URL_SAFE_NO_PAD.decode(input)
10}
11
12/// Decode a Base64-URL string to data and directly store into a mutable `Vec<u8>` reference by concatenating them and return the slice of the decoded data.
13#[inline]
14pub fn decode_to_vec<'a, T: ?Sized + AsRef<[u8]>>(
15    input: &T,
16    output: &'a mut Vec<u8>,
17) -> Result<&'a [u8], base64::DecodeSliceError> {
18    let bytes = input.as_ref();
19
20    let current_length = output.len();
21
22    let original_max_length = ((bytes.len() + 3) >> 2) * 3;
23
24    output.reserve(original_max_length);
25
26    let original_length = {
27        let spare = output.spare_capacity_mut();
28
29        let buf: &mut [u8] =
30            unsafe { from_raw_parts_mut(spare.as_mut_ptr().cast::<u8>(), original_max_length) };
31
32        decode_to_slice(bytes, buf)?.len()
33    };
34
35    #[allow(clippy::uninit_vec)]
36    unsafe {
37        output.set_len(current_length + original_length);
38    }
39
40    Ok(&output[current_length..])
41}
42
43/// Decode a Base64-URL string to data into a slice and return the slice with a valid length.
44#[inline]
45pub fn decode_to_slice<'a, T: ?Sized + AsRef<[u8]>>(
46    input: &T,
47    output: &'a mut [u8],
48) -> Result<&'a [u8], base64::DecodeSliceError> {
49    let length = base64::engine::general_purpose::URL_SAFE_NO_PAD.decode_slice(input, output)?;
50
51    Ok(&output[..length])
52}