base64_url/
encode.rs

1use alloc::{string::String, vec::Vec};
2use core::str::from_utf8_unchecked;
3
4use base64::Engine;
5
6/// Encode data to a Base64-URL string.
7#[inline]
8pub fn encode<T: ?Sized + AsRef<[u8]>>(input: &T) -> String {
9    base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(input)
10}
11
12/// Encode data to a Base64-URL string and directly store to a mutable `String` reference by concatenating them and return the slice of the Base64-URL string. It is usually for generating a URL.
13#[inline]
14pub fn encode_to_string<'a, T: ?Sized + AsRef<[u8]>>(input: &T, output: &'a mut String) -> &'a str {
15    let base64_url = encode_to_vec(input, unsafe { output.as_mut_vec() });
16
17    unsafe { from_utf8_unchecked(base64_url) }
18}
19
20/// Encode data to Base64-URL data and directly store to a mutable `Vec<u8>` reference by concatenating them and return the slice of the Base64-URL data. It is usually for generating a URL.
21#[inline]
22pub fn encode_to_vec<'a, T: ?Sized + AsRef<[u8]>>(input: &T, output: &'a mut Vec<u8>) -> &'a [u8] {
23    let bytes = input.as_ref();
24
25    let current_length = output.len();
26
27    let base64_length = ((bytes.len() << 2) + 2) / 3;
28
29    output.reserve(base64_length);
30
31    #[allow(clippy::uninit_vec)]
32    unsafe {
33        output.set_len(current_length + base64_length);
34    }
35
36    let base64_length = encode_to_slice(bytes, &mut output[current_length..]).unwrap().len();
37
38    unsafe {
39        output.set_len(current_length + base64_length);
40    }
41
42    &output[current_length..]
43}
44
45/// Encode data to a Base64-URL string to a slice and return the slice with a valid length.
46#[inline]
47pub fn encode_to_slice<'a, T: ?Sized + AsRef<[u8]>>(
48    input: &T,
49    output: &'a mut [u8],
50) -> Result<&'a [u8], base64::EncodeSliceError> {
51    let length = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode_slice(input, output)?;
52
53    Ok(&output[..length])
54}