Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ fn do_decode_bench_slice(b: &mut Bencher, &size: &usize) {
});
}

fn do_check_encoded(b: &mut Bencher, &size: &usize) {
let mut v: Vec<u8> = Vec::with_capacity(size * 3 / 4);
fill(&mut v);
let encoded = STANDARD.encode(&v);

b.iter(|| {
STANDARD.check_encoded(&encoded).unwrap();
});
}

fn do_decode_bench_stream(b: &mut Bencher, &size: &usize) {
let mut v: Vec<u8> = Vec::with_capacity(size * 3 / 4);
fill(&mut v);
Expand Down Expand Up @@ -217,6 +227,11 @@ fn decode_benchmarks(c: &mut Criterion, label: &str, byte_sizes: &[usize]) {
size,
do_decode_bench_slice,
)
.bench_with_input(
BenchmarkId::new("check_encoded", size),
size,
do_check_encoded,
)
.bench_with_input(
BenchmarkId::new("decode_stream", size),
size,
Expand Down
48 changes: 45 additions & 3 deletions src/engine/general_purpose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//!
//! See preconfigured engines like [`STANDARD_NO_PAD`] or [`STANDARD_NO_PAD_INDIFFERENT`].
use crate::{
alphabet,
alphabet::Alphabet,
alphabet::{self, Alphabet},
engine::{Config, DecodeMetadata, DecodePaddingMode},
DecodeSliceError,
DecodeError, DecodeSliceError,
};
use core::convert::TryInto;

Expand Down Expand Up @@ -190,6 +189,49 @@ impl super::Engine for GeneralPurpose {
fn config(&self) -> &Self::Config {
&self.config
}

fn check_encoded<T: AsRef<[u8]>>(&self, encoded: T) -> Result<(), DecodeError> {
let input = encoded.as_ref();
let rem = input.len() % 4;
let suffix_start = match (input.len(), rem) {
// there's no prefix, just suffix
(0..4, _) => 0,
// make last partition the suffix
(4.., 0) => input.len() - 4,
// make last incomplete partition the suffix
(4.., rem) => input.len() - rem,
};
// partition the input without suffix
let prefix_input = &input[..suffix_start];

// try to find an invalid value
let invalid_value = prefix_input
.iter()
.position(|&b| self.decode_table[b as usize] == INVALID_VALUE);
if let Some(pos) = invalid_value {
return Err(DecodeError::InvalidByte(pos, prefix_input[pos]));
}

// reuse `decode_suffix`, even tho it writes to a buffer it's not on the hot codepath
let mut output_buffer = [0_u8; 3];
_ = super::decode_suffix(
input,
suffix_start,
&mut output_buffer,
0,
&self.decode_table,
self.config.decode_allow_trailing_bits,
self.config.decode_padding_mode,
)
.map_err(|e| match e {
DecodeSliceError::DecodeError(err) => err,
DecodeSliceError::OutputSliceTooSmall => {
unreachable!("output buffer sized conservatively");
}
})?;

Ok(())
}
}

/// Returns a table mapping a 6-bit index to the ASCII byte encoding of the index
Expand Down
8 changes: 7 additions & 1 deletion src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
use crate::chunked_encoder;
use crate::{
encode::{encode_with_padding, EncodeSliceError},
encoded_len, DecodeError, DecodeSliceError,
encoded_len,
engine::general_purpose::decode_suffix::decode_suffix,
DecodeError, DecodeSliceError,
};
#[cfg(any(feature = "alloc", test))]
use alloc::vec::Vec;
Expand Down Expand Up @@ -416,6 +418,10 @@ pub trait Engine: Send + Sync {

inner(self, input.as_ref(), output)
}

// TODO: more docs
/// Checks for decoding errors without decoding.
fn check_encoded<T: AsRef<[u8]>>(&self, encoded: T) -> Result<(), DecodeError>;
}

/// The minimal level of configuration that engines must support.
Expand Down
5 changes: 5 additions & 0 deletions src/engine/naive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ impl Engine for Naive {
fn config(&self) -> &Self::Config {
&self.config
}

fn check_encoded<T: AsRef<[u8]>>(&self, encoded: T) -> Result<(), DecodeError> {
_ = self.decode(encoded)?;
Ok(())
}
}

pub struct NaiveEstimate {
Expand Down
Loading