Skip to content

Commit

Permalink
cleanup clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
8051Enthusiast committed Jan 27, 2024
1 parent 0c722d6 commit d1cb40f
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 166 deletions.
16 changes: 8 additions & 8 deletions delsum-lib/src/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn find_segments_aligned<S: LinearCheck + ?Sized>(
presums(
summer,
b,
&s,
s,
// since they are already normalized, this should work
start_range.to_unsigned(b.len()).unwrap(),
end_range.to_unsigned(b.len()).unwrap(),
Expand Down Expand Up @@ -358,7 +358,7 @@ fn presums<S: LinearCheck + ?Sized>(
// from the startsums, we substract the init value of the checksum
start_presums.push(summer.add(state.clone(), &neg_init));
}
state = summer.dig_word(state, c as u64);
state = summer.dig_word(state, c);
if end_range.contains(i + step - 1) {
// from the endsums, we finalize them and subtract the given final sum
let endstate = summer.add(summer.finalize(state.clone()), &summer.negate(sum.clone()));
Expand Down Expand Up @@ -427,7 +427,7 @@ impl<Sum: Clone + Eq + Ord + Debug + Send + Sync> PresumSet<Sum> {
// get a permutation vector representing the sort of the presum arrays first by value and then by index

#[cfg(feature = "parallel")]
idxvec.par_sort_unstable_by(|a, b| Self::cmp_idx(&presum, *a, &presum, *b).then(a.cmp(&b)));
idxvec.par_sort_unstable_by(|a, b| Self::cmp_idx(&presum, *a, &presum, *b).then(a.cmp(b)));
#[cfg(not(feature = "parallel"))]
idxvec.sort_unstable_by(|a, b| Self::cmp_idx(&presum, *a, &presum, *b).then(a.cmp(&b)));
Self {
Expand Down Expand Up @@ -638,7 +638,7 @@ nie gefühlten, leichten, dumpfen Schmerz zu fühlen begann.
let sum_1_9_1 = chk.digest(&b"12345678987654321"[..]).unwrap();
assert_eq!(
chk.find_segments(
&[Vec::from(&"a123456789X1235H123456789Y"[..])],
&[Vec::from("a123456789X1235H123456789Y")],
&[sum_1_9.clone()],
Relativity::Start
),
Expand All @@ -647,8 +647,8 @@ nie gefühlten, leichten, dumpfen Schmerz zu fühlen begann.
assert_eq!(
chk.find_segments(
&[
Vec::from(&"XX98765432123456789XXX"[..]),
Vec::from(&"XX12345678987654321XX"[..])
Vec::from("XX98765432123456789XXX"),
Vec::from("XX12345678987654321XX")
],
&[sum_1_9.clone(), sum_9_1.clone()],
Relativity::Start
Expand Down Expand Up @@ -818,7 +818,7 @@ nie gefühlten, leichten, dumpfen Schmerz zu fühlen begann.
}
ret.push(cur_file);
}
ret.sort_by(|a, b| a.len().cmp(&b.len()).then(a.cmp(&b)).reverse());
ret.sort_by(|a, b| a.len().cmp(&b.len()).then(a.cmp(b)).reverse());
ReverseFileSet(ret)
}
fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
Expand Down Expand Up @@ -856,7 +856,7 @@ nie gefühlten, leichten, dumpfen Schmerz zu fühlen begann.
has_appeared = true;
}
for (file, original_check) in &chk_files {
let checksum = modsum_loop.to_bytes(modsum_loop.digest(*file).unwrap());
let checksum = modsum_loop.to_bytes(modsum_loop.digest(file).unwrap());
if &checksum != original_check {
eprint!("expected checksum: ");
for x in original_check {
Expand Down
14 changes: 7 additions & 7 deletions delsum-lib/src/crc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::str::FromStr;
/// For more information on the parameters (and CRCs in general), see "A PAINLESS GUIDE CRC ERROR DETECTION ALGORITHMS"
/// or https://reveng.sourceforge.io/crc-catalogue/legend.htm (which is also a source of parameters for various common algorithms)
#[derive(Clone, Debug)]
pub struct CRCBuilder<Sum: BitNum> {
pub struct CrcBuilder<Sum: BitNum> {
width: Option<usize>,
poly: Option<Sum>,
init: Option<Sum>,
Expand All @@ -41,7 +41,7 @@ pub struct CRCBuilder<Sum: BitNum> {
name: Option<String>,
}

impl<Sum: BitNum> CRCBuilder<Sum> {
impl<Sum: BitNum> CrcBuilder<Sum> {
/// Sets the poly, mandatory
pub fn poly(&mut self, p: Sum) -> &mut Self {
self.poly = Some(p);
Expand Down Expand Up @@ -212,8 +212,8 @@ impl<Sum: BitNum> Display for CRC<Sum> {

impl<Sum: BitNum> CRC<Sum> {
/// Construct a new CRC from parameters, see also the documentation for CRCBuilder.
pub fn with_options() -> CRCBuilder<Sum> {
CRCBuilder {
pub fn with_options() -> CrcBuilder<Sum> {
CrcBuilder {
poly: None,
init: None,
xorout: None,
Expand Down Expand Up @@ -265,9 +265,9 @@ impl<Sum: BitNum> CRC<Sum> {
}
}
}
impl<Sum: BitNum> FromStr for CRCBuilder<Sum> {
impl<Sum: BitNum> FromStr for CrcBuilder<Sum> {
/// See documentation of Fromstr on CRC<Sum>
fn from_str(s: &str) -> Result<CRCBuilder<Sum>, CheckBuilderErr> {
fn from_str(s: &str) -> Result<CrcBuilder<Sum>, CheckBuilderErr> {
let mut crc = CRC::<Sum>::with_options();
for x in KeyValIter::new(s) {
let (current_key, current_val) = match x {
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<Sum: BitNum> FromStr for CRC<Sum> {
///
/// Note: the `residue` parameter is currently ignored.
fn from_str(s: &str) -> Result<CRC<Sum>, CheckBuilderErr> {
CRCBuilder::<Sum>::from_str(s)?.build()
CrcBuilder::<Sum>::from_str(s)?.build()
}
type Err = CheckBuilderErr;
}
Expand Down
97 changes: 44 additions & 53 deletions delsum-lib/src/crc/rev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//!
//! If `init` is not known, it is neccessary to know two checksums of files with different lengths.
//! In case only checksums of files with a set length are required, setting `init = 0` is sufficient.
use super::{CRCBuilder, CRC};
use super::{CrcBuilder, CRC};
use crate::checksum::CheckReverserError;
use crate::endian::{bytes_to_int, int_to_bytes, wordspec_combos, Endian, WordSpec};
use crate::utils::{cart_prod, unresult_iter};
Expand All @@ -28,7 +28,7 @@ use std::pin::Pin;
///
/// The `width` parameter of the builder has to be set.
pub fn reverse_crc<'a>(
spec: &CRCBuilder<u128>,
spec: &CrcBuilder<u128>,
chk_bytes: &'a [(&[u8], Vec<u8>)],
verbosity: u64,
extended_search: bool,
Expand All @@ -39,7 +39,7 @@ pub fn reverse_crc<'a>(
let ref_combinations: Vec<_> = discrete_combos(&spec, extended_search);
ref_combinations
.into_iter()
.map(move |((refin, refout), wordspec)| {
.flat_map(move |((refin, refout), wordspec)| {
unresult_iter(reverse(
&spec,
files.clone(),
Expand All @@ -49,7 +49,6 @@ pub fn reverse_crc<'a>(
wordspec,
))
})
.flatten()
.filter_map(|x| {
// .transpose() but for Err
match x {
Expand All @@ -63,7 +62,7 @@ pub fn reverse_crc<'a>(
/// Parallel version of reverse_crc.
#[cfg(feature = "parallel")]
pub fn reverse_crc_para<'a>(
spec: &CRCBuilder<u128>,
spec: &CrcBuilder<u128>,
chk_bytes: &'a [(&[u8], Vec<u8>)],
verbosity: u64,
extended_search: bool,
Expand Down Expand Up @@ -95,7 +94,7 @@ pub fn reverse_crc_para<'a>(

// find all combinations of refin, refout and wordspecs using all values when a parameter is not given
fn discrete_combos(
spec: &CRCBuilder<u128>,
spec: &CrcBuilder<u128>,
extended_search: bool,
) -> Vec<((bool, bool), WordSpec)> {
let width = spec.width.expect("Missing width argument");
Expand All @@ -112,7 +111,7 @@ fn discrete_combos(
vec![refin]
}
});
let input_endian = spec.input_endian.or_else(|| {
let input_endian = spec.input_endian.or({
Some(match refin {
// big if true since for little, it is equivalent to wordsize=8 which is the same as big
true => Endian::Big,
Expand All @@ -135,7 +134,7 @@ fn discrete_combos(

// wrapper to call rev_from_polys with polynomial arguments
fn reverse<'a>(
spec: &CRCBuilder<u128>,
spec: &CrcBuilder<u128>,
chk_bytes: Vec<(&'a [u8], Vec<u8>)>,
verbosity: u64,
refin: bool,
Expand Down Expand Up @@ -179,7 +178,7 @@ fn reverse<'a>(
None => return Err(None),
};
// sort by reverse file length
polys.sort_by(|(fa, la), (fb, lb)| la.cmp(&lb).then(deg(fa).cmp(&deg(fb)).reverse()));
polys.sort_by(|(fa, la), (fb, lb)| la.cmp(lb).then(deg(fa).cmp(&deg(fb)).reverse()));
// convert parameters to polynomials
let revinfo = RevInfo::from_builder(spec, refin, refout, wordspec);
rev_from_polys(&revinfo, &polys, verbosity).map(|x| x.iter())
Expand All @@ -199,7 +198,7 @@ impl RevInfo {
// this is responsible for converting integer values to polynomial values
// and returning a RevInfo that can be used for further reversing
fn from_builder(
spec: &CRCBuilder<u128>,
spec: &CrcBuilder<u128>,
refin: bool,
refout: bool,
wordspec: WordSpec,
Expand Down Expand Up @@ -250,33 +249,29 @@ impl RevResult {
refout,
wordspec,
} = self;
polys
.into_iter()
.map(move |pol| {
// for each polynomial of degree width, iterate over all solutions of the PrefactorMod
inits
.iter_inits(&pol, &xorout)
.map(move |(poly_p, init_p, xorout_p)| {
// convert polynomial parameters to a CRC<u128>
let poly =
poly_to_u128(&add(&poly_p, &new_poly_shifted(&[1], width as i64)));
let init = poly_to_u128(&init_p);
let xorout = cond_reverse(width as u8, poly_to_u128(&xorout_p), refout);
CRC::<u128>::with_options()
.width(width)
.poly(poly)
.init(init)
.xorout(xorout)
.refin(refin)
.refout(refout)
.inendian(wordspec.input_endian)
.outendian(wordspec.output_endian)
.wordsize(wordspec.wordsize)
.build()
.unwrap()
})
})
.flatten()
polys.into_iter().flat_map(move |pol| {
// for each polynomial of degree width, iterate over all solutions of the PrefactorMod
inits
.iter_inits(&pol, &xorout)
.map(move |(poly_p, init_p, xorout_p)| {
// convert polynomial parameters to a CRC<u128>
let poly = poly_to_u128(&add(&poly_p, &new_poly_shifted(&[1], width as i64)));
let init = poly_to_u128(&init_p);
let xorout = cond_reverse(width as u8, poly_to_u128(&xorout_p), refout);
CRC::<u128>::with_options()
.width(width)
.poly(poly)
.init(init)
.xorout(xorout)
.refin(refin)
.refout(refout)
.inendian(wordspec.input_endian)
.outendian(wordspec.output_endian)
.wordsize(wordspec.wordsize)
.build()
.unwrap()
})
})
}
}

Expand Down Expand Up @@ -354,7 +349,7 @@ fn rev_from_polys(
.collect();
if let Some(init) = &spec.init {
log("removing inits");
remove_inits(&init, &mut polys);
remove_inits(init, &mut polys);
}
log("removing xorouts");
let (polys, mut xorout) = remove_xorouts(&spec.xorout, polys);
Expand Down Expand Up @@ -417,7 +412,7 @@ fn remove_xorouts(
// if we already have xorout, we can subtract it from the files themselves so
// that we have one more to get parameters from
ret_vec.push((add(&prev.0, xorout), prev.1));
(copy_poly(&xorout), InitPlace::None)
(copy_poly(xorout), InitPlace::None)
}
None => (copy_poly(&prev.0), prev.1),
};
Expand Down Expand Up @@ -596,7 +591,7 @@ impl MemoPower {
fn new(hull: &Poly) -> Self {
MemoPower {
prev_power: 0,
prev_ppoly: new_polyrem(&new_poly(&[1]), &hull),
prev_ppoly: new_polyrem(&new_poly(&[1]), hull),
init_fac: new_zero(),
hull: copy_poly(hull),
}
Expand Down Expand Up @@ -628,7 +623,7 @@ impl MemoPower {
}
fn update_hull(&mut self, hull: &Poly) {
self.hull = copy_poly(hull);
self.prev_ppoly = new_polyrem(&self.prev_ppoly.rep(), &hull)
self.prev_ppoly = new_polyrem(&self.prev_ppoly.rep(), hull)
}
}
// describes a set of solutions for unknown*possible % hull
Expand Down Expand Up @@ -656,7 +651,7 @@ impl PrefactorMod {
PrefactorMod {
unknown,
possible,
hull: copy_poly(&hull),
hull: copy_poly(hull),
}
}

Expand Down Expand Up @@ -809,10 +804,7 @@ fn find_init(
for (p, l) in polys {
power.update_init_fac(&l);
let file_solutions = PrefactorMod::new_file(p, &mut power, hull.as_mut());
ret = match file_solutions
.map(|f| ret.merge(f, hull.as_mut()))
.flatten()
{
ret = match file_solutions.and_then(|f| ret.merge(f, hull.as_mut())) {
Some(valid) => valid,
None => return PrefactorMod::empty(),
}
Expand Down Expand Up @@ -869,7 +861,7 @@ fn find_prod_comb(
break;
}
ret[inc_deg].push(copy_poly(&q));
for (j, el) in retcopy[0..=width as usize - inc_deg].iter().enumerate() {
for (j, el) in retcopy[0..=width - inc_deg].iter().enumerate() {
for m in el {
ret[j + inc_deg].push(mul(&q, m));
}
Expand All @@ -890,7 +882,7 @@ fn bytes_to_poly(
) -> Option<PolyPtr> {
let new_bytes = reorder_poly_bytes(bytes, refin, wordspec);
let mut poly = new_poly_shifted(&new_bytes, width as i64);
let sum = bytes_to_int(&checksum, wordspec.output_endian);
let sum = bytes_to_int(checksum, wordspec.output_endian);
let check_mask = 1u128.checked_shl(width as u32).unwrap_or(0).wrapping_sub(1);
if (!check_mask & sum) != 0 {
return None;
Expand All @@ -904,15 +896,14 @@ fn reorder_poly_bytes(bytes: &[u8], refin: bool, wordspec: WordSpec) -> Vec<u8>
wordspec
.iter_words(bytes)
.rev()
.map(|n| {
.flat_map(|n| {
let n_ref = if refin {
n.reverse_bits() >> (64 - wordspec.wordsize)
} else {
n
};
int_to_bytes(n_ref, Endian::Little, wordspec.wordsize)
})
.flatten()
.collect()
}

Expand All @@ -939,9 +930,9 @@ fn poly_to_u128(poly: &Poly) -> u128 {
mod tests {
use super::*;
use crate::checksum::tests::ReverseFileSet;
use crate::crc::{CRCBuilder, CRC};
use crate::crc::{CrcBuilder, CRC};
use quickcheck::{Arbitrary, Gen, TestResult};
impl Arbitrary for CRCBuilder<u128> {
impl Arbitrary for CrcBuilder<u128> {
fn arbitrary(g: &mut Gen) -> Self {
let width = (u8::arbitrary(g) % 128) + 1;
let poly;
Expand Down Expand Up @@ -976,7 +967,7 @@ mod tests {
#[quickcheck]
fn qc_crc_rev(
files: ReverseFileSet,
crc_build: CRCBuilder<u128>,
crc_build: CrcBuilder<u128>,
known: (bool, bool, bool, bool, bool),
wordspec_known: (bool, bool),
) -> TestResult {
Expand Down
6 changes: 3 additions & 3 deletions delsum-lib/src/endian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub(crate) fn int_to_bytes<N: BitNum>(n: N, e: Endian, bits: usize) -> Vec<u8> {
let mut ret = Vec::new();
for x in 0..n_bytes {
let shift = match e {
Endian::Little => 8 * x as usize,
Endian::Big => 8 * (n_bytes - 1 - x) as usize,
Endian::Little => 8 * x,
Endian::Big => 8 * (n_bytes - 1 - x),
};
if let Ok(a) = (n >> shift & N::from(0xffu8)).try_into() {
ret.push(a)
Expand Down Expand Up @@ -112,7 +112,7 @@ impl WordSpec {
bytes_to_int(s, self.output_endian)
}
pub fn iter_words(self, bytes: &'_ [u8]) -> impl DoubleEndedIterator<Item = u64> + '_ {
(0..(bytes.len() / self.word_bytes() as usize))
(0..(bytes.len() / self.word_bytes()))
.map(move |i| &bytes[self.word_bytes() * i..self.word_bytes() * (i + 1)])
.map(move |x| bytes_to_int(x, self.input_endian))
}
Expand Down
Loading

0 comments on commit d1cb40f

Please sign in to comment.