This commit is contained in:
Steven Fackler 2019-11-21 17:10:34 -08:00
parent b5eafedf17
commit 18552f1f1d
1 changed files with 8 additions and 6 deletions

View File

@ -3,10 +3,10 @@
//! See manual page of [`EVP_EncodeInit`] for more information on the specific base64 variant. //! See manual page of [`EVP_EncodeInit`] for more information on the specific base64 variant.
//! //!
//! [`EVP_EncodeInit`]: https://www.openssl.org/docs/man1.1.1/man3/EVP_EncodeInit.html //! [`EVP_EncodeInit`]: https://www.openssl.org/docs/man1.1.1/man3/EVP_EncodeInit.html
use cvt_n;
use error::ErrorStack; use error::ErrorStack;
use ffi; use ffi;
use libc::c_int; use libc::c_int;
use cvt_n;
/// Encodes a given block of bytes to base64. /// Encodes a given block of bytes to base64.
/// ///
@ -19,8 +19,7 @@ pub fn encode_block(src: &[u8]) -> String {
let src_len = src.len() as c_int; let src_len = src.len() as c_int;
let len = encoded_len(src_len).unwrap(); let len = encoded_len(src_len).unwrap();
let mut out = Vec::new(); let mut out = Vec::with_capacity(len as usize);
out.reserve(len as usize);
// SAFETY: `encoded_len` ensures space for 4 output characters // SAFETY: `encoded_len` ensures space for 4 output characters
// for every 3 input bytes including padding and nul terminator. // for every 3 input bytes including padding and nul terminator.
@ -46,8 +45,7 @@ pub fn decode_block(src: &str) -> Result<Vec<u8>, ErrorStack> {
let src_len = src.len() as c_int; let src_len = src.len() as c_int;
let len = decoded_len(src_len).unwrap(); let len = decoded_len(src_len).unwrap();
let mut out = Vec::new(); let mut out = Vec::with_capacity(len as usize);
out.reserve(len as usize);
// SAFETY: `decoded_len` ensures space for 3 output bytes // SAFETY: `decoded_len` ensures space for 3 output bytes
// for every 4 input characters including padding. // for every 4 input characters including padding.
@ -55,7 +53,11 @@ pub fn decode_block(src: &str) -> Result<Vec<u8>, ErrorStack> {
// leading and trailing whitespace, but never more. // leading and trailing whitespace, but never more.
// `EVP_DecodeBlock` will only write to not read from `out`. // `EVP_DecodeBlock` will only write to not read from `out`.
unsafe { unsafe {
let out_len = cvt_n(ffi::EVP_DecodeBlock(out.as_mut_ptr(), src.as_ptr(), src_len))?; let out_len = cvt_n(ffi::EVP_DecodeBlock(
out.as_mut_ptr(),
src.as_ptr(),
src_len,
))?;
out.set_len(out_len as usize); out.set_len(out_len as usize);
} }