Skip to content

Commit c3c77fd

Browse files
author
Andrea Calabrese
committed
basenc: fix merge issues and removed code
Signed-off-by: Andrea Calabrese <andrea.calabrese@amarulasolutions.com>
1 parent c7cf018 commit c3c77fd

File tree

2 files changed

+36
-89
lines changed

2 files changed

+36
-89
lines changed

src/uu/base32/src/base_common.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use clap::{Arg, ArgAction, Command};
99
use std::ffi::OsString;
1010
use std::fs::File;
11-
use std::io::{self, ErrorKind, Read};
11+
use std::io::{self, ErrorKind, Read, Write};
1212
use std::path::{Path, PathBuf};
1313
use uucore::display::Quotable;
1414
use uucore::encoding::{
@@ -479,15 +479,40 @@ pub mod fast_decode {
479479
};
480480

481481
// Start of helper functions
482-
fn alphabet_lookup(alphabet: &[u8]) -> [bool; 256] {
483-
// Precompute O(1) membership checks so we can validate every byte before decoding.
484-
let mut table = [false; 256];
482+
fn alphabet_to_table(alphabet: &[u8], ignore_garbage: bool) -> [bool; 256] {
483+
// If `ignore_garbage` is enabled, all characters outside the alphabet are ignored
484+
// If it is not enabled, only '\n' and '\r' are ignored
485+
if ignore_garbage {
486+
// Note: "false" here
487+
let mut table = [false; 256];
485488

486-
for &byte in alphabet {
487-
table[usize::from(byte)] = true;
488-
}
489+
// Pass through no characters except those in the alphabet
490+
for ue in alphabet {
491+
let us = usize::from(*ue);
492+
493+
// Should not have been set yet
494+
assert!(!table[us]);
495+
496+
table[us] = true;
497+
}
498+
499+
table
500+
} else {
501+
// Note: "true" here
502+
let mut table = [true; 256];
503+
504+
// Pass through all characters except '\n' and '\r'
505+
for ue in [b'\n', b'\r'] {
506+
let us = usize::from(ue);
489507

490-
table
508+
// Should not have been set yet
509+
assert!(table[us]);
510+
511+
table[us] = false;
512+
}
513+
514+
table
515+
}
491516
}
492517

493518
fn decode_in_chunks_to_buffer(
@@ -509,39 +534,6 @@ pub mod fast_decode {
509534
Ok(())
510535
}
511536

512-
fn flush_ready_chunks(
513-
buffer: &mut Vec<u8>,
514-
block_limit: usize,
515-
valid_multiple: usize,
516-
supports_fast_decode_and_encode: &dyn SupportsFastDecodeAndEncode,
517-
decoded_buffer: &mut Vec<u8>,
518-
output: &mut dyn Write,
519-
) -> UResult<()> {
520-
// While at least one full decode block is buffered, keep draining
521-
// it and never yield more than block_limit per chunk.
522-
while buffer.len() >= valid_multiple {
523-
let take = buffer.len().min(block_limit);
524-
let aligned_take = take - (take % valid_multiple);
525-
526-
if aligned_take < valid_multiple {
527-
break;
528-
}
529-
530-
decode_in_chunks_to_buffer(
531-
supports_fast_decode_and_encode,
532-
&buffer[..aligned_take],
533-
decoded_buffer,
534-
)?;
535-
536-
write_to_output(decoded_buffer, output)?;
537-
538-
buffer.drain(..aligned_take);
539-
}
540-
541-
Ok(())
542-
}
543-
// End of helper functions
544-
545537
pub fn fast_decode(
546538
input: &mut dyn Read,
547539
output: &mut dyn Write,

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

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,6 @@ pub struct Base64SimdWrapper {
2121
}
2222

2323
impl Base64SimdWrapper {
24-
fn decode_with_standard(input: &[u8], output: &mut Vec<u8>) -> Result<(), ()> {
25-
match base64_simd::STANDARD.decode_to_vec(input) {
26-
Ok(decoded_bytes) => {
27-
output.extend_from_slice(&decoded_bytes);
28-
Ok(())
29-
}
30-
Err(_) => Err(()),
31-
}
32-
}
33-
34-
fn decode_with_no_pad(input: &[u8], output: &mut Vec<u8>) -> Result<(), ()> {
35-
match base64_simd::STANDARD_NO_PAD.decode_to_vec(input) {
36-
Ok(decoded_bytes) => {
37-
output.extend_from_slice(&decoded_bytes);
38-
Ok(())
39-
}
40-
Err(_) => Err(()),
41-
}
42-
}
43-
4424
pub fn new(
4525
valid_decoding_multiple: usize,
4626
unpadded_multiple: usize,
@@ -552,36 +532,11 @@ impl SupportsFastDecodeAndEncode for Base32Wrapper {
552532
self.inner.valid_decoding_multiple()
553533
}
554534

555-
fn pad_remainder(&self, remainder: &[u8]) -> Option<PadResult> {
556-
if remainder.is_empty() || remainder.contains(&b'=') {
557-
return None;
558-
}
559-
560-
const VALID_REMAINDERS: [usize; 4] = [2, 4, 5, 7];
561-
562-
let mut len = remainder.len();
563-
let mut trimmed = false;
564-
565-
while len > 0 && !VALID_REMAINDERS.contains(&len) {
566-
len -= 1;
567-
trimmed = true;
568-
}
569-
570-
if len == 0 {
571-
return None;
572-
}
573-
574-
let mut padded = remainder[..len].to_vec();
575-
let missing = self.valid_decoding_multiple() - padded.len();
576-
padded.extend(std::iter::repeat_n(b'=', missing));
577-
578-
Some(PadResult {
579-
chunk: padded,
580-
had_invalid_tail: trimmed,
581-
})
535+
fn should_buffer_decoding(&self) -> bool {
536+
true
582537
}
583538

584-
fn supports_partial_decode(&self) -> bool {
539+
fn should_buffer_encoding(&self) -> bool {
585540
true
586541
}
587542
}

0 commit comments

Comments
 (0)