usecrate::engine::{general_purpose::STANDARD, DecodeEstimate, Engine}; #[cfg(any(feature = "alloc", test))] use alloc::vec::Vec; use core::fmt; #[cfg(any(feature = "std", test))] use std::error;
/// Errors that can occur while decoding. #[derive(Clone, Debug, PartialEq, Eq)] pubenum DecodeError { /// An invalid byte was found in the input. The offset and offending byte are provided. /// /// Padding characters (`=`) interspersed in the encoded form are invalid, as they may only /// be present as the last 0-2 bytes of input. /// /// This error may also indicate that extraneous trailing input bytes are present, causing /// otherwise valid padding to no longer be the last bytes of input.
InvalidByte(usize, u8), /// The length of the input, as measured in valid base64 symbols, is invalid. /// There must be 2-4 symbols in the last input quad.
InvalidLength(usize), /// The last non-padding input symbol's encoded 6 bits have nonzero bits that will be discarded. /// This is indicative of corrupted or truncated Base64. /// Unlike [DecodeError::InvalidByte], which reports symbols that aren't in the alphabet, /// this error is for symbols that are in the alphabet but represent nonsensical encodings.
InvalidLastSymbol(usize, u8), /// The nature of the padding was not as configured: absent or incorrect when it must be /// canonical, or present when it must be absent, etc.
InvalidPadding,
}
#[cfg(any(feature = "std", test))] impl error::Error for DecodeError {}
/// Errors that can occur while decoding into a slice. #[derive(Clone, Debug, PartialEq, Eq)] pubenum DecodeSliceError { /// A [DecodeError] occurred
DecodeError(DecodeError), /// The provided slice is too small.
OutputSliceTooSmall,
}
/// Returns a conservative estimate of the decoded size of `encoded_len` base64 symbols (rounded up /// to the next group of 3 decoded bytes). /// /// The resulting length will be a safe choice for the size of a decode buffer, but may have up to /// 2 trailing bytes that won't end up being needed. /// /// # Examples /// /// ``` /// use base64::decoded_len_estimate; /// /// assert_eq!(3, decoded_len_estimate(1)); /// assert_eq!(3, decoded_len_estimate(2)); /// assert_eq!(3, decoded_len_estimate(3)); /// assert_eq!(3, decoded_len_estimate(4)); /// // start of the next quad of encoded symbols /// assert_eq!(6, decoded_len_estimate(5)); /// ``` pub fn decoded_len_estimate(encoded_len: usize) -> usize {
STANDARD
.internal_decoded_len_estimate(encoded_len)
.decoded_len_estimate()
}
#[cfg(test)] mod tests { usesuper::*; usecrate::{
alphabet,
engine::{general_purpose, Config, GeneralPurpose},
tests::{assert_encode_sanity, random_engine},
}; use rand::{
distributions::{Distribution, Uniform},
Rng, SeedableRng,
};
// decode into the non-empty buf
engine
.decode_vec(&encoded_data, &mut decoded_with_prefix)
.unwrap(); // also decode into the empty buf
engine
.decode_vec(&encoded_data, &mut decoded_without_prefix)
.unwrap();
#[test]
fn decode_engine_estimation_works_for_various_lengths() { let engine = GeneralPurpose::new(&alphabet::STANDARD, general_purpose::NO_PAD);
for num_prefix_quads in 0..100 {
for suffix in &["AA", "AAA", "AAAA"] { letmut prefix = "AAAA".repeat(num_prefix_quads);
prefix.push_str(suffix); // make sure no overflow (and thus a panic) occurs let res = engine.decode(prefix);
assert!(res.is_ok());
}
}
}
#[test]
fn decode_slice_output_length_errors() {
for num_quads in 1..100 { let input = "AAAA".repeat(num_quads); letmut vec = vec![0; (num_quads - 1) * 3];
assert_eq!(
DecodeSliceError::OutputSliceTooSmall,
STANDARD.decode_slice(&input, &mut vec).unwrap_err()
);
vec.push(0);
assert_eq!(
DecodeSliceError::OutputSliceTooSmall,
STANDARD.decode_slice(&input, &mut vec).unwrap_err()
);
vec.push(0);
assert_eq!(
DecodeSliceError::OutputSliceTooSmall,
STANDARD.decode_slice(&input, &mut vec).unwrap_err()
);
vec.push(0); // now it works
assert_eq!(
num_quads * 3,
STANDARD.decode_slice(&input, &mut vec).unwrap()
);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.