Skip to content

Commit bcbc669

Browse files
Minor cleanup
1 parent 604a3ca commit bcbc669

File tree

7 files changed

+221
-188
lines changed

7 files changed

+221
-188
lines changed

src/engine/fast_portable/decode.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::engine::fast_portable::{DecodePaddingMode, INVALID_VALUE};
2-
use crate::engine::DecodeEstimate;
3-
use crate::{DecodeError, PAD_BYTE};
1+
use crate::{
2+
engine::{fast_portable::INVALID_VALUE, DecodeEstimate, DecodePaddingMode},
3+
DecodeError, PAD_BYTE,
4+
};
45

56
// decode logic operates on chunks of 8 input bytes without padding
67
const INPUT_CHUNK_LEN: usize = 8;

src/engine/fast_portable/decode_suffix.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
engine::fast_portable::{DecodePaddingMode, INVALID_VALUE},
2+
engine::{fast_portable::INVALID_VALUE, DecodePaddingMode},
33
DecodeError, PAD_BYTE,
44
};
55

@@ -17,7 +17,7 @@ pub(crate) fn decode_suffix(
1717
decode_allow_trailing_bits: bool,
1818
padding_mode: DecodePaddingMode,
1919
) -> Result<usize, DecodeError> {
20-
// Finally, decode any leftovers that aren't a complete input block of 8 bytes.
20+
// Decode any leftovers that aren't a complete input block of 8 bytes.
2121
// Use a u64 as a stack-resident 8 byte buffer.
2222
let mut leftover_bits: u64 = 0;
2323
let mut morsels_in_leftover = 0;
@@ -46,7 +46,7 @@ pub(crate) fn decode_suffix(
4646
let bad_padding_index = start_of_leftovers
4747
+ if padding_bytes > 0 {
4848
// If we've already seen padding, report the first padding index.
49-
// This is to be consistent with the faster logic above: it will report an
49+
// This is to be consistent with the normal decode logic: it will report an
5050
// error on the first padding character (since it doesn't expect to see
5151
// anything but actual encoded data).
5252
// This could only happen if the padding started in the previous quad since

src/engine/fast_portable/mod.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! Provides the [FastPortable] engine and associated config types.
2-
use crate::alphabet::Alphabet;
3-
use crate::engine::Config;
4-
use crate::DecodeError;
2+
use crate::{
3+
alphabet::Alphabet,
4+
engine::{Config, DecodePaddingMode},
5+
DecodeError,
6+
};
57
use core::convert::TryInto;
68

79
mod decode;
@@ -309,18 +311,6 @@ impl FastPortableConfig {
309311
}
310312
}
311313

312-
// TODO move to top level of decode?
313-
/// Controls how pad bytes are handled when decoding.
314-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
315-
pub enum DecodePaddingMode {
316-
/// Canonical padding is allowed, but any fewer padding bytes than that is also allowed.
317-
Indifferent,
318-
/// Padding must be canonical (0, 1, or 2 `=` as needed to produce a 4 byte suffix).
319-
RequireCanonical,
320-
/// Padding must be absent -- for when you want predictable padding, without any wasted bytes.
321-
RequireNone,
322-
}
323-
324314
impl Default for FastPortableConfig {
325315
/// Delegates to [FastPortableConfig::new].
326316
fn default() -> Self {

src/engine/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub trait Engine: Send + Sync {
6060
///
6161
/// Decoding must not write any bytes into the output slice other than the decoded data.
6262
///
63-
/// Non-canonical trailing bits in the final tokens and padding must be reported as errors,
64-
/// though implementations may choose to allow altering that behavior via config.
63+
/// Non-canonical trailing bits in the final tokens or non-canonical padding must be reported as
64+
/// errors unless the engine is configured otherwise.
6565
fn decode(
6666
&self,
6767
input: &[u8],
@@ -100,3 +100,17 @@ pub trait DecodeEstimate {
100100
/// A [FastPortable] engine using the [crate::alphabet::STANDARD] base64 alphabet and [crate::engine::fast_portable::PAD] config.
101101
pub const DEFAULT_ENGINE: FastPortable =
102102
FastPortable::from(&alphabet::STANDARD, fast_portable::PAD);
103+
104+
/// Controls how pad bytes are handled when decoding.
105+
///
106+
/// Each [Engine] must support at least the behavior indicated by
107+
/// [DecodePaddingMode::RequireCanonical], and may support other modes.
108+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
109+
pub enum DecodePaddingMode {
110+
/// Canonical padding is allowed, but any fewer padding bytes than that is also allowed.
111+
Indifferent,
112+
/// Padding must be canonical (0, 1, or 2 `=` as needed to produce a 4 byte suffix).
113+
RequireCanonical,
114+
/// Padding must be absent -- for when you want predictable padding, without any wasted bytes.
115+
RequireNone,
116+
}

src/engine/naive.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
use crate::alphabet::Alphabet;
2-
use crate::engine::fast_portable::{decode_table, encode_table, DecodePaddingMode};
3-
use crate::engine::{fast_portable, Config, DecodeEstimate, Engine};
4-
use crate::{DecodeError, PAD_BYTE};
1+
use crate::{
2+
alphabet::Alphabet,
3+
engine::{
4+
fast_portable::{self, decode_table, encode_table},
5+
Config, DecodeEstimate, DecodePaddingMode, Engine,
6+
},
7+
DecodeError, PAD_BYTE,
8+
};
59
use alloc::ops::BitOr;
610
use std::ops::{BitAnd, Shl, Shr};
711

0 commit comments

Comments
 (0)