Skip to content

Commit 98abe7a

Browse files
author
Andrea Calabrese
committed
Removing padding as input of Base64SimdWrapper
This should remove the dependency we have in knowing whether the final message has padding or not. This is the first step to not have a ahead-of-time loading of the entire message to encode/decode, and allow for streaming. Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com>
1 parent ebd8377 commit 98abe7a

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

src/uu/base32/src/base_common.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,10 @@ pub fn get_supports_fast_decode_and_encode(
264264
b"0123456789ABCDEFGHIJKLMNOPQRSTUV=",
265265
)),
266266
Format::Base64 => {
267-
let alphabet: &[u8] = if has_padding {
268-
&b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/="[..]
269-
} else {
270-
&b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/"[..]
271-
};
272-
let use_padding = !decode || has_padding;
273267
Box::from(Base64SimdWrapper::new(
274-
use_padding,
275268
BASE64_VALID_DECODING_MULTIPLE,
276269
BASE64_UNPADDED_MULTIPLE,
277-
alphabet,
270+
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=",
278271
))
279272
}
280273
Format::Base64Url => Box::from(EncodingWrapper::new(

src/uucore/src/lib/features/encoding.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ use std::collections::VecDeque;
1515
// SIMD base64 wrapper
1616
pub struct Base64SimdWrapper {
1717
pub alphabet: &'static [u8],
18-
pub use_padding: bool,
1918
pub unpadded_multiple: usize,
2019
pub valid_decoding_multiple: usize,
2120
}
2221

2322
impl Base64SimdWrapper {
2423
pub fn new(
25-
use_padding: bool,
2624
valid_decoding_multiple: usize,
2725
unpadded_multiple: usize,
2826
alphabet: &'static [u8],
@@ -33,7 +31,6 @@ impl Base64SimdWrapper {
3331

3432
Self {
3533
alphabet,
36-
use_padding,
3734
unpadded_multiple,
3835
valid_decoding_multiple,
3936
}
@@ -46,31 +43,35 @@ impl SupportsFastDecodeAndEncode for Base64SimdWrapper {
4643
}
4744

4845
fn decode_into_vec(&self, input: &[u8], output: &mut Vec<u8>) -> UResult<()> {
49-
let decoded = if self.use_padding {
50-
base64_simd::STANDARD.decode_to_vec(input)
51-
} else {
52-
base64_simd::STANDARD_NO_PAD.decode_to_vec(input)
53-
};
46+
// Padding always comes at the end, so at most once. No_PAD should be
47+
// called most of the times
48+
let decoded = base64_simd::STANDARD_NO_PAD.decode_to_vec(input);
5449

5550
match decoded {
5651
Ok(decoded_bytes) => {
5752
output.extend_from_slice(&decoded_bytes);
5853
Ok(())
5954
}
6055
Err(_) => {
61-
// Restore original length on error
62-
output.truncate(output.len());
63-
Err(USimpleError::new(1, "error: invalid input".to_owned()))
56+
// Check if the padding works
57+
let decoded_2 = base64_simd::STANDARD.decode_to_vec(input);
58+
match decoded_2 {
59+
Ok(decoded_bytes_2) => {
60+
output.extend_from_slice(&decoded_bytes_2);
61+
Ok(())
62+
}
63+
Err(_) => {
64+
// Restore original length on error
65+
output.truncate(output.len());
66+
Err(USimpleError::new(1, "error: invalid input".to_owned()))
67+
}
68+
}
6469
}
6570
}
6671
}
6772

6873
fn encode_to_vec_deque(&self, input: &[u8], output: &mut VecDeque<u8>) -> UResult<()> {
69-
let encoded = if self.use_padding {
70-
base64_simd::STANDARD.encode_to_string(input)
71-
} else {
72-
base64_simd::STANDARD_NO_PAD.encode_to_string(input)
73-
};
74+
let encoded = base64_simd::STANDARD.encode_to_string(input);
7475

7576
output.extend(encoded.as_bytes());
7677

0 commit comments

Comments
 (0)