Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallpierce committed Nov 30, 2022
1 parent 604a3ca commit bcbc669
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 188 deletions.
7 changes: 4 additions & 3 deletions src/engine/fast_portable/decode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::engine::fast_portable::{DecodePaddingMode, INVALID_VALUE};
use crate::engine::DecodeEstimate;
use crate::{DecodeError, PAD_BYTE};
use crate::{
engine::{fast_portable::INVALID_VALUE, DecodeEstimate, DecodePaddingMode},
DecodeError, PAD_BYTE,
};

// decode logic operates on chunks of 8 input bytes without padding
const INPUT_CHUNK_LEN: usize = 8;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/fast_portable/decode_suffix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
engine::fast_portable::{DecodePaddingMode, INVALID_VALUE},
engine::{fast_portable::INVALID_VALUE, DecodePaddingMode},
DecodeError, PAD_BYTE,
};

Expand All @@ -17,7 +17,7 @@ pub(crate) fn decode_suffix(
decode_allow_trailing_bits: bool,
padding_mode: DecodePaddingMode,
) -> Result<usize, DecodeError> {
// Finally, decode any leftovers that aren't a complete input block of 8 bytes.
// Decode any leftovers that aren't a complete input block of 8 bytes.
// Use a u64 as a stack-resident 8 byte buffer.
let mut leftover_bits: u64 = 0;
let mut morsels_in_leftover = 0;
Expand Down Expand Up @@ -46,7 +46,7 @@ pub(crate) fn decode_suffix(
let bad_padding_index = start_of_leftovers
+ if padding_bytes > 0 {
// If we've already seen padding, report the first padding index.
// This is to be consistent with the faster logic above: it will report an
// This is to be consistent with the normal decode logic: it will report an
// error on the first padding character (since it doesn't expect to see
// anything but actual encoded data).
// This could only happen if the padding started in the previous quad since
Expand Down
20 changes: 5 additions & 15 deletions src/engine/fast_portable/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Provides the [FastPortable] engine and associated config types.
use crate::alphabet::Alphabet;
use crate::engine::Config;
use crate::DecodeError;
use crate::{
alphabet::Alphabet,
engine::{Config, DecodePaddingMode},
DecodeError,
};
use core::convert::TryInto;

mod decode;
Expand Down Expand Up @@ -309,18 +311,6 @@ impl FastPortableConfig {
}
}

// TODO move to top level of decode?
/// Controls how pad bytes are handled when decoding.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DecodePaddingMode {
/// Canonical padding is allowed, but any fewer padding bytes than that is also allowed.
Indifferent,
/// Padding must be canonical (0, 1, or 2 `=` as needed to produce a 4 byte suffix).
RequireCanonical,
/// Padding must be absent -- for when you want predictable padding, without any wasted bytes.
RequireNone,
}

impl Default for FastPortableConfig {
/// Delegates to [FastPortableConfig::new].
fn default() -> Self {
Expand Down
18 changes: 16 additions & 2 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub trait Engine: Send + Sync {
///
/// Decoding must not write any bytes into the output slice other than the decoded data.
///
/// Non-canonical trailing bits in the final tokens and padding must be reported as errors,
/// though implementations may choose to allow altering that behavior via config.
/// Non-canonical trailing bits in the final tokens or non-canonical padding must be reported as
/// errors unless the engine is configured otherwise.
fn decode(
&self,
input: &[u8],
Expand Down Expand Up @@ -100,3 +100,17 @@ pub trait DecodeEstimate {
/// A [FastPortable] engine using the [crate::alphabet::STANDARD] base64 alphabet and [crate::engine::fast_portable::PAD] config.
pub const DEFAULT_ENGINE: FastPortable =
FastPortable::from(&alphabet::STANDARD, fast_portable::PAD);

/// Controls how pad bytes are handled when decoding.
///
/// Each [Engine] must support at least the behavior indicated by
/// [DecodePaddingMode::RequireCanonical], and may support other modes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DecodePaddingMode {
/// Canonical padding is allowed, but any fewer padding bytes than that is also allowed.
Indifferent,
/// Padding must be canonical (0, 1, or 2 `=` as needed to produce a 4 byte suffix).
RequireCanonical,
/// Padding must be absent -- for when you want predictable padding, without any wasted bytes.
RequireNone,
}
12 changes: 8 additions & 4 deletions src/engine/naive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::alphabet::Alphabet;
use crate::engine::fast_portable::{decode_table, encode_table, DecodePaddingMode};
use crate::engine::{fast_portable, Config, DecodeEstimate, Engine};
use crate::{DecodeError, PAD_BYTE};
use crate::{
alphabet::Alphabet,
engine::{
fast_portable::{self, decode_table, encode_table},
Config, DecodeEstimate, DecodePaddingMode, Engine,
},
DecodeError, PAD_BYTE,
};
use alloc::ops::BitOr;
use std::ops::{BitAnd, Shl, Shr};

Expand Down
Loading

0 comments on commit bcbc669

Please sign in to comment.