Function base64::encoded_len
source · Expand description
Calculate the base64 encoded length for a given input length, optionally including any appropriate padding bytes.
Returns None
if the encoded length can’t be represented in usize
. This will happen for
input lengths in approximately the top quarter of the range of usize
.
Examples found in repository?
src/engine/mod.rs (line 127)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
fn encode<T: AsRef<[u8]>>(&self, input: T) -> String {
let encoded_size = encoded_len(input.as_ref().len(), self.config().encode_padding())
.expect("integer overflow when calculating buffer size");
let mut buf = vec![0; encoded_size];
encode_with_padding(input.as_ref(), &mut buf[..], self, encoded_size);
String::from_utf8(buf).expect("Invalid UTF8")
}
/// Encode arbitrary octets as base64 into a supplied `String`.
/// Writes into the supplied `String`, which may allocate if its internal buffer isn't big enough.
///
/// # Example
///
/// ```rust
/// use base64::Engine as _;
/// const URL_SAFE_ENGINE: base64::engine::GeneralPurpose =
/// base64::engine::GeneralPurpose::new(
/// &base64::alphabet::URL_SAFE,
/// base64::engine::general_purpose::NO_PAD);
/// fn main() {
/// let mut buf = String::new();
/// base64::engine::STANDARD.encode_string(b"hello world~", &mut buf);
/// println!("{}", buf);
///
/// buf.clear();
/// URL_SAFE_ENGINE.encode_string(b"hello internet~", &mut buf);
/// println!("{}", buf);
/// }
/// ```
#[cfg(any(feature = "alloc", feature = "std", test))]
fn encode_string<T: AsRef<[u8]>>(&self, input: T, output_buf: &mut String) {
let input_bytes = input.as_ref();
{
let mut sink = chunked_encoder::StringSink::new(output_buf);
chunked_encoder::ChunkedEncoder::new(self)
.encode(input_bytes, &mut sink)
.expect("Writing to a String shouldn't fail");
}
}
/// Encode arbitrary octets as base64 into a supplied slice.
/// Writes into the supplied output buffer.
///
/// This is useful if you wish to avoid allocation entirely (e.g. encoding into a stack-resident
/// or statically-allocated buffer).
///
/// # Example
///
/// ```rust
/// use base64::{engine, Engine as _};
/// let s = b"hello internet!";
/// let mut buf = Vec::new();
/// // make sure we'll have a slice big enough for base64 + padding
/// buf.resize(s.len() * 4 / 3 + 4, 0);
///
/// let bytes_written = engine::STANDARD.encode_slice(s, &mut buf).unwrap();
///
/// // shorten our vec down to just what was written
/// buf.truncate(bytes_written);
///
/// assert_eq!(s, engine::STANDARD.decode(&buf).unwrap().as_slice());
/// ```
fn encode_slice<T: AsRef<[u8]>>(
&self,
input: T,
output_buf: &mut [u8],
) -> Result<usize, EncodeSliceError> {
let input_bytes = input.as_ref();
let encoded_size = encoded_len(input_bytes.len(), self.config().encode_padding())
.expect("usize overflow when calculating buffer size");
if output_buf.len() < encoded_size {
return Err(EncodeSliceError::OutputSliceTooSmall);
}
let b64_output = &mut output_buf[0..encoded_size];
encode_with_padding(input_bytes, b64_output, self, encoded_size);
Ok(encoded_size)
}