Skip to content

Commit caa1837

Browse files
committed
Add custom params support to the software fallback
1 parent 8c97d7e commit caa1837

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

src/arch/software.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::consts::CRC_64_NVME;
88
use crate::CrcAlgorithm;
99
use crate::CrcParams;
10-
use crc::Table;
10+
use crc::{Algorithm, Table};
1111

1212
const RUST_CRC32_AIXM: crc::Crc<u32, Table<16>> =
1313
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_AIXM);
@@ -78,6 +78,23 @@ pub(crate) fn update(state: u64, data: &[u8], params: CrcParams) -> u64 {
7878
CrcAlgorithm::Crc32Mef => RUST_CRC32_MEF,
7979
CrcAlgorithm::Crc32Mpeg2 => RUST_CRC32_MPEG_2,
8080
CrcAlgorithm::Crc32Xfer => RUST_CRC32_XFER,
81+
CrcAlgorithm::Crc32Custom => {
82+
let algorithm: Algorithm<u32> = Algorithm {
83+
width: params.width as u8,
84+
poly: params.poly as u32,
85+
init: params.init as u32,
86+
refin: params.refin,
87+
refout: params.refout,
88+
xorout: params.xorout as u32,
89+
check: params.check as u32,
90+
residue: 0x00000000, // unused in this context
91+
};
92+
93+
// ugly, but the crc crate is difficult to work with...
94+
let static_algorithm = Box::leak(Box::new(algorithm));
95+
96+
crc::Crc::<u32, Table<16>>::new(static_algorithm)
97+
}
8198
_ => panic!("Invalid algorithm for u32 CRC"),
8299
};
83100
update_u32(state as u32, data, params) as u64
@@ -91,6 +108,23 @@ pub(crate) fn update(state: u64, data: &[u8], params: CrcParams) -> u64 {
91108
CrcAlgorithm::Crc64Redis => RUST_CRC64_REDIS,
92109
CrcAlgorithm::Crc64We => RUST_CRC64_WE,
93110
CrcAlgorithm::Crc64Xz => RUST_CRC64_XZ,
111+
CrcAlgorithm::Crc64Custom => {
112+
let algorithm: Algorithm<u64> = Algorithm {
113+
width: params.width as u8,
114+
poly: params.poly as u64,
115+
init: params.init as u64,
116+
refin: params.refin,
117+
refout: params.refout,
118+
xorout: params.xorout as u64,
119+
check: params.check as u64,
120+
residue: 0x0000000000000000, // unused in this context
121+
};
122+
123+
// ugly, but the crc crate is difficult to work with...
124+
let static_algorithm = Box::leak(Box::new(algorithm));
125+
126+
crc::Crc::<u64, Table<16>>::new(static_algorithm)
127+
}
94128
_ => panic!("Invalid algorithm for u64 CRC"),
95129
};
96130
update_u64(state, data, params)

0 commit comments

Comments
 (0)