use base64::{decoded_len_estimate, engine::general_purpose::STANDARD, Engine as _};
use zeroize::Zeroizing;
use crate::{error::Base64Error, MAX_BASE64_INPUT_LEN};
pub fn base64_to_bytes(input: &str) -> Result<Vec<u8>, Base64Error> {
if input.len() > MAX_BASE64_INPUT_LEN {
return Err(Base64Error::InputTooLarge);
}
let mut output = Zeroizing::new(vec![0_u8; decoded_len_estimate(input.len())]);
let length = STANDARD
.decode_slice(input.as_bytes(), output.as_mut_slice())
.map_err(|_| Base64Error::Invalid)?;
output.truncate(length);
Ok(core::mem::take(&mut *output))
}