| 1 |
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0. |
| 2 |
|
| 3 |
//! This module contains a software fallback for unsupported architectures. |
| 4 |
|
| 5 |
use crate::consts::CRC_64_NVME; |
| 6 |
use crate::CrcAlgorithm; |
| 7 |
use crate::CrcParams; |
| 8 |
#[cfg(feature = "alloc")] |
| 9 |
use crc::Algorithm; |
| 10 |
use crc::Table; |
| 11 |
|
| 12 |
// Caching for custom CRC algorithms to prevent repeated memory leaks |
| 13 |
#[cfg(feature = "alloc")] |
| 14 |
#[cfg(feature = "std")] |
| 15 |
use std::collections::HashMap; |
| 16 |
#[cfg(feature = "alloc")] |
| 17 |
#[cfg(feature = "std")] |
| 18 |
use std::sync::{Mutex, OnceLock}; |
| 19 |
|
| 20 |
#[cfg(feature = "alloc")] |
| 21 |
#[cfg(all(not(feature = "std"), feature = "cache"))] |
| 22 |
use hashbrown::HashMap; |
| 23 |
#[cfg(feature = "alloc")] |
| 24 |
#[cfg(all(not(feature = "std"), feature = "cache"))] |
| 25 |
use spin::{Mutex, Once}; |
| 26 |
|
| 27 |
// Cache key types for custom algorithms |
| 28 |
#[cfg(feature = "alloc")] |
| 29 |
#[cfg(any(feature = "std", feature = "cache"))] |
| 30 |
type Crc16Key = (u16, u16, bool, bool, u16, u16); |
| 31 |
#[cfg(feature = "alloc")] |
| 32 |
#[cfg(any(feature = "std", feature = "cache"))] |
| 33 |
type Crc32Key = (u32, u32, bool, bool, u32, u32); |
| 34 |
#[cfg(feature = "alloc")] |
| 35 |
#[cfg(any(feature = "std", feature = "cache"))] |
| 36 |
type Crc64Key = (u64, u64, bool, bool, u64, u64); |
| 37 |
|
| 38 |
// Global caches for custom algorithms (std version) |
| 39 |
#[cfg(feature = "alloc")] |
| 40 |
#[cfg(feature = "std")] |
| 41 |
static CUSTOM_CRC16_CACHE: OnceLock<Mutex<HashMap<Crc16Key, &'static Algorithm<u16>>>> = |
| 42 |
OnceLock::new(); |
| 43 |
#[cfg(feature = "alloc")] |
| 44 |
#[cfg(feature = "std")] |
| 45 |
static CUSTOM_CRC32_CACHE: OnceLock<Mutex<HashMap<Crc32Key, &'static Algorithm<u32>>>> = |
| 46 |
OnceLock::new(); |
| 47 |
#[cfg(feature = "alloc")] |
| 48 |
#[cfg(feature = "std")] |
| 49 |
static CUSTOM_CRC64_CACHE: OnceLock<Mutex<HashMap<Crc64Key, &'static Algorithm<u64>>>> = |
| 50 |
OnceLock::new(); |
| 51 |
|
| 52 |
// Global caches for custom algorithms (no_std + cache version) |
| 53 |
#[cfg(feature = "alloc")] |
| 54 |
#[cfg(all(not(feature = "std"), feature = "cache"))] |
| 55 |
static CUSTOM_CRC16_CACHE: Once<Mutex<HashMap<Crc16Key, &'static Algorithm<u16>>>> = Once::new(); |
| 56 |
#[cfg(feature = "alloc")] |
| 57 |
#[cfg(all(not(feature = "std"), feature = "cache"))] |
| 58 |
static CUSTOM_CRC32_CACHE: Once<Mutex<HashMap<Crc32Key, &'static Algorithm<u32>>>> = Once::new(); |
| 59 |
#[cfg(feature = "alloc")] |
| 60 |
#[cfg(all(not(feature = "std"), feature = "cache"))] |
| 61 |
static CUSTOM_CRC64_CACHE: Once<Mutex<HashMap<Crc64Key, &'static Algorithm<u64>>>> = Once::new(); |
| 62 |
|
| 63 |
#[allow(unused)] |
| 64 |
const RUST_CRC16_ARC: crc::Crc<u16, Table<16>> = crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_ARC); |
| 65 |
|
| 66 |
#[allow(unused)] |
| 67 |
const RUST_CRC16_CDMA2000: crc::Crc<u16, Table<16>> = |
| 68 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_CDMA2000); |
| 69 |
|
| 70 |
#[allow(unused)] |
| 71 |
const RUST_CRC16_CMS: crc::Crc<u16, Table<16>> = crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_CMS); |
| 72 |
|
| 73 |
#[allow(unused)] |
| 74 |
const RUST_CRC16_DDS_110: crc::Crc<u16, Table<16>> = |
| 75 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_DDS_110); |
| 76 |
|
| 77 |
#[allow(unused)] |
| 78 |
const RUST_CRC16_DECT_R: crc::Crc<u16, Table<16>> = |
| 79 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_DECT_R); |
| 80 |
|
| 81 |
#[allow(unused)] |
| 82 |
const RUST_CRC16_DECT_X: crc::Crc<u16, Table<16>> = |
| 83 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_DECT_X); |
| 84 |
|
| 85 |
#[allow(unused)] |
| 86 |
const RUST_CRC16_DNP: crc::Crc<u16, Table<16>> = crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_DNP); |
| 87 |
|
| 88 |
#[allow(unused)] |
| 89 |
const RUST_CRC16_EN_13757: crc::Crc<u16, Table<16>> = |
| 90 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_EN_13757); |
| 91 |
|
| 92 |
#[allow(unused)] |
| 93 |
const RUST_CRC16_GENIBUS: crc::Crc<u16, Table<16>> = |
| 94 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_GENIBUS); |
| 95 |
|
| 96 |
#[allow(unused)] |
| 97 |
const RUST_CRC16_GSM: crc::Crc<u16, Table<16>> = crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_GSM); |
| 98 |
|
| 99 |
#[allow(unused)] |
| 100 |
const RUST_CRC16_IBM_3740: crc::Crc<u16, Table<16>> = |
| 101 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_IBM_3740); |
| 102 |
|
| 103 |
#[allow(unused)] |
| 104 |
const RUST_CRC16_IBM_SDLC: crc::Crc<u16, Table<16>> = |
| 105 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_IBM_SDLC); |
| 106 |
|
| 107 |
#[allow(unused)] |
| 108 |
const RUST_CRC16_ISO_IEC_14443_3_A: crc::Crc<u16, Table<16>> = |
| 109 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_ISO_IEC_14443_3_A); |
| 110 |
|
| 111 |
#[allow(unused)] |
| 112 |
const RUST_CRC16_KERMIT: crc::Crc<u16, Table<16>> = |
| 113 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_KERMIT); |
| 114 |
|
| 115 |
#[allow(unused)] |
| 116 |
const RUST_CRC16_LJ1200: crc::Crc<u16, Table<16>> = |
| 117 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_LJ1200); |
| 118 |
|
| 119 |
#[allow(unused)] |
| 120 |
const RUST_CRC16_M17: crc::Crc<u16, Table<16>> = crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_M17); |
| 121 |
|
| 122 |
#[allow(unused)] |
| 123 |
const RUST_CRC16_MAXIM_DOW: crc::Crc<u16, Table<16>> = |
| 124 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_MAXIM_DOW); |
| 125 |
|
| 126 |
#[allow(unused)] |
| 127 |
const RUST_CRC16_MCRF4XX: crc::Crc<u16, Table<16>> = |
| 128 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_MCRF4XX); |
| 129 |
|
| 130 |
#[allow(unused)] |
| 131 |
const RUST_CRC16_MODBUS: crc::Crc<u16, Table<16>> = |
| 132 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_MODBUS); |
| 133 |
|
| 134 |
#[allow(unused)] |
| 135 |
const RUST_CRC16_NRSC_5: crc::Crc<u16, Table<16>> = |
| 136 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_NRSC_5); |
| 137 |
|
| 138 |
#[allow(unused)] |
| 139 |
const RUST_CRC16_OPENSAFETY_A: crc::Crc<u16, Table<16>> = |
| 140 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_OPENSAFETY_A); |
| 141 |
|
| 142 |
#[allow(unused)] |
| 143 |
const RUST_CRC16_OPENSAFETY_B: crc::Crc<u16, Table<16>> = |
| 144 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_OPENSAFETY_B); |
| 145 |
|
| 146 |
#[allow(unused)] |
| 147 |
const RUST_CRC16_PROFIBUS: crc::Crc<u16, Table<16>> = |
| 148 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_PROFIBUS); |
| 149 |
|
| 150 |
#[allow(unused)] |
| 151 |
const RUST_CRC16_RIELLO: crc::Crc<u16, Table<16>> = |
| 152 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_RIELLO); |
| 153 |
|
| 154 |
#[allow(unused)] |
| 155 |
const RUST_CRC16_SPI_FUJITSU: crc::Crc<u16, Table<16>> = |
| 156 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_SPI_FUJITSU); |
| 157 |
|
| 158 |
#[allow(unused)] |
| 159 |
const RUST_CRC16_T10_DIF: crc::Crc<u16, Table<16>> = |
| 160 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_T10_DIF); |
| 161 |
|
| 162 |
#[allow(unused)] |
| 163 |
const RUST_CRC16_TELEDISK: crc::Crc<u16, Table<16>> = |
| 164 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_TELEDISK); |
| 165 |
|
| 166 |
#[allow(unused)] |
| 167 |
const RUST_CRC16_TMS37157: crc::Crc<u16, Table<16>> = |
| 168 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_TMS37157); |
| 169 |
|
| 170 |
#[allow(unused)] |
| 171 |
const RUST_CRC16_UMTS: crc::Crc<u16, Table<16>> = |
| 172 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_UMTS); |
| 173 |
|
| 174 |
#[allow(unused)] |
| 175 |
const RUST_CRC16_USB: crc::Crc<u16, Table<16>> = crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_USB); |
| 176 |
|
| 177 |
#[allow(unused)] |
| 178 |
const RUST_CRC16_XMODEM: crc::Crc<u16, Table<16>> = |
| 179 |
crc::Crc::<u16, Table<16>>::new(&crc::CRC_16_XMODEM); |
| 180 |
|
| 181 |
#[allow(unused)] |
| 182 |
const RUST_CRC32_AIXM: crc::Crc<u32, Table<16>> = |
| 183 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_AIXM); |
| 184 |
|
| 185 |
#[allow(unused)] |
| 186 |
const RUST_CRC32_AUTOSAR: crc::Crc<u32, Table<16>> = |
| 187 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_AUTOSAR); |
| 188 |
|
| 189 |
#[allow(unused)] |
| 190 |
const RUST_CRC32_BASE91_D: crc::Crc<u32, Table<16>> = |
| 191 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_BASE91_D); |
| 192 |
|
| 193 |
#[allow(unused)] |
| 194 |
const RUST_CRC32_BZIP2: crc::Crc<u32, Table<16>> = |
| 195 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_BZIP2); |
| 196 |
|
| 197 |
#[allow(unused)] |
| 198 |
const RUST_CRC32_CD_ROM_EDC: crc::Crc<u32, Table<16>> = |
| 199 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_CD_ROM_EDC); |
| 200 |
|
| 201 |
#[allow(unused)] |
| 202 |
const RUST_CRC32_CKSUM: crc::Crc<u32, Table<16>> = |
| 203 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_CKSUM); |
| 204 |
|
| 205 |
#[allow(unused)] |
| 206 |
const RUST_CRC32_ISCSI: crc::Crc<u32, Table<16>> = |
| 207 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_ISCSI); |
| 208 |
|
| 209 |
#[allow(unused)] |
| 210 |
const RUST_CRC32_ISO_HDLC: crc::Crc<u32, Table<16>> = |
| 211 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_ISO_HDLC); |
| 212 |
|
| 213 |
#[allow(unused)] |
| 214 |
const RUST_CRC32_JAMCRC: crc::Crc<u32, Table<16>> = |
| 215 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_JAMCRC); |
| 216 |
|
| 217 |
#[allow(unused)] |
| 218 |
const RUST_CRC32_MEF: crc::Crc<u32, Table<16>> = crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_MEF); |
| 219 |
|
| 220 |
#[allow(unused)] |
| 221 |
const RUST_CRC32_MPEG_2: crc::Crc<u32, Table<16>> = |
| 222 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_MPEG_2); |
| 223 |
|
| 224 |
#[allow(unused)] |
| 225 |
const RUST_CRC32_XFER: crc::Crc<u32, Table<16>> = |
| 226 |
crc::Crc::<u32, Table<16>>::new(&crc::CRC_32_XFER); |
| 227 |
|
| 228 |
#[allow(unused)] |
| 229 |
const RUST_CRC64_ECMA_182: crc::Crc<u64, Table<16>> = |
| 230 |
crc::Crc::<u64, Table<16>>::new(&crc::CRC_64_ECMA_182); |
| 231 |
|
| 232 |
#[allow(unused)] |
| 233 |
const RUST_CRC64_GO_ISO: crc::Crc<u64, Table<16>> = |
| 234 |
crc::Crc::<u64, Table<16>>::new(&crc::CRC_64_GO_ISO); |
| 235 |
|
| 236 |
#[allow(unused)] |
| 237 |
const RUST_CRC64_MS: crc::Crc<u64, Table<16>> = crc::Crc::<u64, Table<16>>::new(&crc::CRC_64_MS); |
| 238 |
|
| 239 |
#[allow(unused)] |
| 240 |
const RUST_CRC64_NVME: crc::Crc<u64, Table<16>> = crc::Crc::<u64, Table<16>>::new(&CRC_64_NVME); |
| 241 |
|
| 242 |
#[allow(unused)] |
| 243 |
const RUST_CRC64_REDIS: crc::Crc<u64, Table<16>> = |
| 244 |
crc::Crc::<u64, Table<16>>::new(&crc::CRC_64_REDIS); |
| 245 |
|
| 246 |
#[allow(unused)] |
| 247 |
const RUST_CRC64_WE: crc::Crc<u64, Table<16>> = crc::Crc::<u64, Table<16>>::new(&crc::CRC_64_WE); |
| 248 |
|
| 249 |
#[allow(unused)] |
| 250 |
const RUST_CRC64_XZ: crc::Crc<u64, Table<16>> = crc::Crc::<u64, Table<16>>::new(&crc::CRC_64_XZ); |
| 251 |
|
| 252 |
#[allow(unused)] |
| 253 |
// Dispatch function that handles the generic case |
| 254 |
#[allow(deprecated)] |
| 255 |
pub(crate) fn update(state: u64, data: &[u8], params: &CrcParams) -> u64 { |
| 256 |
match params.width { |
| 257 |
16 => { |
| 258 |
let params = match params.algorithm { |
| 259 |
CrcAlgorithm::Crc16Arc => RUST_CRC16_ARC, |
| 260 |
CrcAlgorithm::Crc16Cdma2000 => RUST_CRC16_CDMA2000, |
| 261 |
CrcAlgorithm::Crc16Cms => RUST_CRC16_CMS, |
| 262 |
CrcAlgorithm::Crc16Dds110 => RUST_CRC16_DDS_110, |
| 263 |
CrcAlgorithm::Crc16DectR => RUST_CRC16_DECT_R, |
| 264 |
CrcAlgorithm::Crc16DectX => RUST_CRC16_DECT_X, |
| 265 |
CrcAlgorithm::Crc16Dnp => RUST_CRC16_DNP, |
| 266 |
CrcAlgorithm::Crc16En13757 => RUST_CRC16_EN_13757, |
| 267 |
CrcAlgorithm::Crc16Genibus => RUST_CRC16_GENIBUS, |
| 268 |
CrcAlgorithm::Crc16Gsm => RUST_CRC16_GSM, |
| 269 |
CrcAlgorithm::Crc16Ibm3740 => RUST_CRC16_IBM_3740, |
| 270 |
CrcAlgorithm::Crc16IbmSdlc => RUST_CRC16_IBM_SDLC, |
| 271 |
CrcAlgorithm::Crc16IsoIec144433A => RUST_CRC16_ISO_IEC_14443_3_A, |
| 272 |
CrcAlgorithm::Crc16Kermit => RUST_CRC16_KERMIT, |
| 273 |
CrcAlgorithm::Crc16Lj1200 => RUST_CRC16_LJ1200, |
| 274 |
CrcAlgorithm::Crc16M17 => RUST_CRC16_M17, |
| 275 |
CrcAlgorithm::Crc16MaximDow => RUST_CRC16_MAXIM_DOW, |
| 276 |
CrcAlgorithm::Crc16Mcrf4xx => RUST_CRC16_MCRF4XX, |
| 277 |
CrcAlgorithm::Crc16Modbus => RUST_CRC16_MODBUS, |
| 278 |
CrcAlgorithm::Crc16Nrsc5 => RUST_CRC16_NRSC_5, |
| 279 |
CrcAlgorithm::Crc16OpensafetyA => RUST_CRC16_OPENSAFETY_A, |
| 280 |
CrcAlgorithm::Crc16OpensafetyB => RUST_CRC16_OPENSAFETY_B, |
| 281 |
CrcAlgorithm::Crc16Profibus => RUST_CRC16_PROFIBUS, |
| 282 |
CrcAlgorithm::Crc16Riello => RUST_CRC16_RIELLO, |
| 283 |
CrcAlgorithm::Crc16SpiFujitsu => RUST_CRC16_SPI_FUJITSU, |
| 284 |
CrcAlgorithm::Crc16T10Dif => RUST_CRC16_T10_DIF, |
| 285 |
CrcAlgorithm::Crc16Teledisk => RUST_CRC16_TELEDISK, |
| 286 |
CrcAlgorithm::Crc16Tms37157 => RUST_CRC16_TMS37157, |
| 287 |
CrcAlgorithm::Crc16Umts => RUST_CRC16_UMTS, |
| 288 |
CrcAlgorithm::Crc16Usb => RUST_CRC16_USB, |
| 289 |
CrcAlgorithm::Crc16Xmodem => RUST_CRC16_XMODEM, |
| 290 |
CrcAlgorithm::CrcCustom => { |
| 291 |
#[cfg(feature = "alloc")] |
| 292 |
{ |
| 293 |
extern crate alloc; |
| 294 |
use alloc::boxed::Box; |
| 295 |
|
| 296 |
#[cfg(any(feature = "std", feature = "cache"))] |
| 297 |
{ |
| 298 |
let key: Crc16Key = ( |
| 299 |
params.poly as u16, |
| 300 |
params.init as u16, |
| 301 |
params.refin, |
| 302 |
params.refout, |
| 303 |
params.xorout as u16, |
| 304 |
params.check as u16, |
| 305 |
); |
| 306 |
|
| 307 |
#[cfg(feature = "std")] |
| 308 |
{ |
| 309 |
let cache = |
| 310 |
CUSTOM_CRC16_CACHE.get_or_init(|| Mutex::new(HashMap::new())); |
| 311 |
let mut cache_guard = cache.lock().unwrap(); |
| 312 |
|
| 313 |
let static_algorithm = |
| 314 |
cache_guard.entry(key).or_insert_with(|| { |
| 315 |
let algorithm: Algorithm<u16> = Algorithm { |
| 316 |
width: params.width, |
| 317 |
poly: params.poly as u16, |
| 318 |
init: params.init as u16, |
| 319 |
refin: params.refin, |
| 320 |
refout: params.refout, |
| 321 |
xorout: params.xorout as u16, |
| 322 |
check: params.check as u16, |
| 323 |
residue: 0x0000, |
| 324 |
}; |
| 325 |
Box::leak(Box::new(algorithm)) |
| 326 |
}); |
| 327 |
|
| 328 |
crc::Crc::<u16, Table<16>>::new(static_algorithm) |
| 329 |
} |
| 330 |
|
| 331 |
#[cfg(all(feature = "cache", not(feature = "std")))] |
| 332 |
{ |
| 333 |
let cache = |
| 334 |
CUSTOM_CRC16_CACHE.call_once(|| Mutex::new(HashMap::new())); |
| 335 |
let mut cache_guard = cache.lock(); |
| 336 |
|
| 337 |
let static_algorithm = |
| 338 |
cache_guard.entry(key).or_insert_with(|| { |
| 339 |
let algorithm: Algorithm<u16> = Algorithm { |
| 340 |
width: params.width, |
| 341 |
poly: params.poly as u16, |
| 342 |
init: params.init as u16, |
| 343 |
refin: params.refin, |
| 344 |
refout: params.refout, |
| 345 |
xorout: params.xorout as u16, |
| 346 |
check: params.check as u16, |
| 347 |
residue: 0x0000, |
| 348 |
}; |
| 349 |
Box::leak(Box::new(algorithm)) |
| 350 |
}); |
| 351 |
|
| 352 |
crc::Crc::<u16, Table<16>>::new(static_algorithm) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
// Without cache, just leak (no_std without cache feature) |
| 357 |
#[cfg(not(any(feature = "std", feature = "cache")))] |
| 358 |
{ |
| 359 |
let algorithm: Algorithm<u16> = Algorithm { |
| 360 |
width: params.width, |
| 361 |
poly: params.poly as u16, |
| 362 |
init: params.init as u16, |
| 363 |
refin: params.refin, |
| 364 |
refout: params.refout, |
| 365 |
xorout: params.xorout as u16, |
| 366 |
check: params.check as u16, |
| 367 |
residue: 0x0000, |
| 368 |
}; |
| 369 |
|
| 370 |
let static_algorithm = Box::leak(Box::new(algorithm)); |
| 371 |
|
| 372 |
crc::Crc::<u16, Table<16>>::new(static_algorithm) |
| 373 |
} |
| 374 |
} |
| 375 |
#[cfg(not(feature = "alloc"))] |
| 376 |
panic!("Custom CRC parameters require the 'alloc' feature") |
| 377 |
} |
| 378 |
_ => panic!("Invalid algorithm for u16 CRC"), |
| 379 |
}; |
| 380 |
update_u16(state as u16, data, params) as u64 |
| 381 |
} |
| 382 |
32 => { |
| 383 |
let params = match params.algorithm { |
| 384 |
CrcAlgorithm::Crc32Aixm => RUST_CRC32_AIXM, |
| 385 |
CrcAlgorithm::Crc32Autosar => RUST_CRC32_AUTOSAR, |
| 386 |
CrcAlgorithm::Crc32Base91D => RUST_CRC32_BASE91_D, |
| 387 |
CrcAlgorithm::Crc32Bzip2 => RUST_CRC32_BZIP2, |
| 388 |
CrcAlgorithm::Crc32CdRomEdc => RUST_CRC32_CD_ROM_EDC, |
| 389 |
CrcAlgorithm::Crc32Cksum => RUST_CRC32_CKSUM, |
| 390 |
CrcAlgorithm::Crc32Iscsi => RUST_CRC32_ISCSI, |
| 391 |
CrcAlgorithm::Crc32IsoHdlc => RUST_CRC32_ISO_HDLC, |
| 392 |
CrcAlgorithm::Crc32Jamcrc => RUST_CRC32_JAMCRC, |
| 393 |
CrcAlgorithm::Crc32Mef => RUST_CRC32_MEF, |
| 394 |
CrcAlgorithm::Crc32Mpeg2 => RUST_CRC32_MPEG_2, |
| 395 |
CrcAlgorithm::Crc32Xfer => RUST_CRC32_XFER, |
| 396 |
CrcAlgorithm::Crc32Custom => { |
| 397 |
#[cfg(feature = "alloc")] |
| 398 |
{ |
| 399 |
extern crate alloc; |
| 400 |
use alloc::boxed::Box; |
| 401 |
|
| 402 |
// Use cache if std or cache feature is enabled |
| 403 |
#[cfg(any(feature = "std", feature = "cache"))] |
| 404 |
{ |
| 405 |
let key: Crc32Key = ( |
| 406 |
params.poly as u32, |
| 407 |
params.init as u32, |
| 408 |
params.refin, |
| 409 |
params.refout, |
| 410 |
params.xorout as u32, |
| 411 |
params.check as u32, |
| 412 |
); |
| 413 |
|
| 414 |
#[cfg(feature = "std")] |
| 415 |
{ |
| 416 |
let cache = |
| 417 |
CUSTOM_CRC32_CACHE.get_or_init(|| Mutex::new(HashMap::new())); |
| 418 |
let mut cache_guard = cache.lock().unwrap(); |
| 419 |
|
| 420 |
let static_algorithm = |
| 421 |
cache_guard.entry(key).or_insert_with(|| { |
| 422 |
let algorithm = Algorithm { |
| 423 |
width: params.width, |
| 424 |
poly: params.poly as u32, |
| 425 |
init: params.init as u32, |
| 426 |
refin: params.refin, |
| 427 |
refout: params.refout, |
| 428 |
xorout: params.xorout as u32, |
| 429 |
check: params.check as u32, |
| 430 |
residue: 0x00000000, |
| 431 |
}; |
| 432 |
Box::leak(Box::new(algorithm)) |
| 433 |
}); |
| 434 |
|
| 435 |
crc::Crc::<u32, Table<16>>::new(static_algorithm) |
| 436 |
} |
| 437 |
|
| 438 |
#[cfg(all(not(feature = "std"), feature = "cache"))] |
| 439 |
{ |
| 440 |
let cache = |
| 441 |
CUSTOM_CRC32_CACHE.call_once(|| Mutex::new(HashMap::new())); |
| 442 |
let mut cache_guard = cache.lock(); |
| 443 |
|
| 444 |
let static_algorithm = |
| 445 |
cache_guard.entry(key).or_insert_with(|| { |
| 446 |
let algorithm = Algorithm { |
| 447 |
width: params.width, |
| 448 |
poly: params.poly as u32, |
| 449 |
init: params.init as u32, |
| 450 |
refin: params.refin, |
| 451 |
refout: params.refout, |
| 452 |
xorout: params.xorout as u32, |
| 453 |
check: params.check as u32, |
| 454 |
residue: 0x00000000, |
| 455 |
}; |
| 456 |
Box::leak(Box::new(algorithm)) |
| 457 |
}); |
| 458 |
|
| 459 |
crc::Crc::<u32, Table<16>>::new(static_algorithm) |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
// Without cache, just leak (no_std without cache feature) |
| 464 |
#[cfg(not(any(feature = "std", feature = "cache")))] |
| 465 |
{ |
| 466 |
let algorithm: Algorithm<u32> = Algorithm { |
| 467 |
width: params.width, |
| 468 |
poly: params.poly as u32, |
| 469 |
init: params.init as u32, |
| 470 |
refin: params.refin, |
| 471 |
refout: params.refout, |
| 472 |
xorout: params.xorout as u32, |
| 473 |
check: params.check as u32, |
| 474 |
residue: 0x00000000, // unused in this context |
| 475 |
}; |
| 476 |
|
| 477 |
// ugly, but the crc crate is difficult to work with... |
| 478 |
let static_algorithm = Box::leak(Box::new(algorithm)); |
| 479 |
|
| 480 |
crc::Crc::<u32, Table<16>>::new(static_algorithm) |
| 481 |
} |
| 482 |
} |
| 483 |
#[cfg(not(feature = "alloc"))] |
| 484 |
panic!("Custom CRC parameters require the 'alloc' feature") |
| 485 |
} |
| 486 |
CrcAlgorithm::CrcCustom => { |
| 487 |
#[cfg(feature = "alloc")] |
| 488 |
{ |
| 489 |
extern crate alloc; |
| 490 |
use alloc::boxed::Box; |
| 491 |
|
| 492 |
#[cfg(any(feature = "std", feature = "cache"))] |
| 493 |
{ |
| 494 |
let key: Crc32Key = ( |
| 495 |
params.poly as u32, |
| 496 |
params.init as u32, |
| 497 |
params.refin, |
| 498 |
params.refout, |
| 499 |
params.xorout as u32, |
| 500 |
params.check as u32, |
| 501 |
); |
| 502 |
|
| 503 |
#[cfg(feature = "std")] |
| 504 |
{ |
| 505 |
let cache = |
| 506 |
CUSTOM_CRC32_CACHE.get_or_init(|| Mutex::new(HashMap::new())); |
| 507 |
let mut cache_guard = cache.lock().unwrap(); |
| 508 |
|
| 509 |
let static_algorithm = |
| 510 |
cache_guard.entry(key).or_insert_with(|| { |
| 511 |
let algorithm: Algorithm<u32> = Algorithm { |
| 512 |
width: params.width, |
| 513 |
poly: params.poly as u32, |
| 514 |
init: params.init as u32, |
| 515 |
refin: params.refin, |
| 516 |
refout: params.refout, |
| 517 |
xorout: params.xorout as u32, |
| 518 |
check: params.check as u32, |
| 519 |
residue: 0x00000000, |
| 520 |
}; |
| 521 |
Box::leak(Box::new(algorithm)) |
| 522 |
}); |
| 523 |
|
| 524 |
crc::Crc::<u32, Table<16>>::new(static_algorithm) |
| 525 |
} |
| 526 |
|
| 527 |
#[cfg(all(feature = "cache", not(feature = "std")))] |
| 528 |
{ |
| 529 |
let cache = |
| 530 |
CUSTOM_CRC32_CACHE.call_once(|| Mutex::new(HashMap::new())); |
| 531 |
let mut cache_guard = cache.lock(); |
| 532 |
|
| 533 |
let static_algorithm = |
| 534 |
cache_guard.entry(key).or_insert_with(|| { |
| 535 |
let algorithm: Algorithm<u32> = Algorithm { |
| 536 |
width: params.width, |
| 537 |
poly: params.poly as u32, |
| 538 |
init: params.init as u32, |
| 539 |
refin: params.refin, |
| 540 |
refout: params.refout, |
| 541 |
xorout: params.xorout as u32, |
| 542 |
check: params.check as u32, |
| 543 |
residue: 0x00000000, |
| 544 |
}; |
| 545 |
Box::leak(Box::new(algorithm)) |
| 546 |
}); |
| 547 |
|
| 548 |
crc::Crc::<u32, Table<16>>::new(static_algorithm) |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
// Without cache, just leak (no_std without cache feature) |
| 553 |
#[cfg(not(any(feature = "std", feature = "cache")))] |
| 554 |
{ |
| 555 |
let algorithm: Algorithm<u32> = Algorithm { |
| 556 |
width: params.width, |
| 557 |
poly: params.poly as u32, |
| 558 |
init: params.init as u32, |
| 559 |
refin: params.refin, |
| 560 |
refout: params.refout, |
| 561 |
xorout: params.xorout as u32, |
| 562 |
check: params.check as u32, |
| 563 |
residue: 0x00000000, |
| 564 |
}; |
| 565 |
|
| 566 |
let static_algorithm = Box::leak(Box::new(algorithm)); |
| 567 |
|
| 568 |
crc::Crc::<u32, Table<16>>::new(static_algorithm) |
| 569 |
} |
| 570 |
} |
| 571 |
#[cfg(not(feature = "alloc"))] |
| 572 |
panic!("Custom CRC parameters require the 'alloc' feature") |
| 573 |
} |
| 574 |
_ => panic!("Invalid algorithm for u32 CRC"), |
| 575 |
}; |
| 576 |
update_u32(state as u32, data, params) as u64 |
| 577 |
} |
| 578 |
64 => { |
| 579 |
let params = match params.algorithm { |
| 580 |
CrcAlgorithm::Crc64Ecma182 => RUST_CRC64_ECMA_182, |
| 581 |
CrcAlgorithm::Crc64GoIso => RUST_CRC64_GO_ISO, |
| 582 |
CrcAlgorithm::Crc64Ms => RUST_CRC64_MS, |
| 583 |
CrcAlgorithm::Crc64Nvme => RUST_CRC64_NVME, |
| 584 |
CrcAlgorithm::Crc64Redis => RUST_CRC64_REDIS, |
| 585 |
CrcAlgorithm::Crc64We => RUST_CRC64_WE, |
| 586 |
CrcAlgorithm::Crc64Xz => RUST_CRC64_XZ, |
| 587 |
CrcAlgorithm::Crc64Custom => { |
| 588 |
#[cfg(feature = "alloc")] |
| 589 |
{ |
| 590 |
extern crate alloc; |
| 591 |
use alloc::boxed::Box; |
| 592 |
|
| 593 |
// Use cache if std or cache feature is enabled |
| 594 |
#[cfg(any(feature = "std", feature = "cache"))] |
| 595 |
{ |
| 596 |
let key: Crc64Key = ( |
| 597 |
params.poly, |
| 598 |
params.init, |
| 599 |
params.refin, |
| 600 |
params.refout, |
| 601 |
params.xorout, |
| 602 |
params.check, |
| 603 |
); |
| 604 |
|
| 605 |
#[cfg(feature = "std")] |
| 606 |
{ |
| 607 |
let cache = |
| 608 |
CUSTOM_CRC64_CACHE.get_or_init(|| Mutex::new(HashMap::new())); |
| 609 |
let mut cache_guard = cache.lock().unwrap(); |
| 610 |
|
| 611 |
let static_algorithm = |
| 612 |
cache_guard.entry(key).or_insert_with(|| { |
| 613 |
let algorithm = Algorithm { |
| 614 |
width: params.width, |
| 615 |
poly: params.poly, |
| 616 |
init: params.init, |
| 617 |
refin: params.refin, |
| 618 |
refout: params.refout, |
| 619 |
xorout: params.xorout, |
| 620 |
check: params.check, |
| 621 |
residue: 0x0000000000000000, |
| 622 |
}; |
| 623 |
Box::leak(Box::new(algorithm)) |
| 624 |
}); |
| 625 |
|
| 626 |
crc::Crc::<u64, Table<16>>::new(static_algorithm) |
| 627 |
} |
| 628 |
|
| 629 |
#[cfg(all(not(feature = "std"), feature = "cache"))] |
| 630 |
{ |
| 631 |
let cache = |
| 632 |
CUSTOM_CRC64_CACHE.call_once(|| Mutex::new(HashMap::new())); |
| 633 |
let mut cache_guard = cache.lock(); |
| 634 |
|
| 635 |
let static_algorithm = |
| 636 |
cache_guard.entry(key).or_insert_with(|| { |
| 637 |
let algorithm = Algorithm { |
| 638 |
width: params.width, |
| 639 |
poly: params.poly, |
| 640 |
init: params.init, |
| 641 |
refin: params.refin, |
| 642 |
refout: params.refout, |
| 643 |
xorout: params.xorout, |
| 644 |
check: params.check, |
| 645 |
residue: 0x0000000000000000, |
| 646 |
}; |
| 647 |
Box::leak(Box::new(algorithm)) |
| 648 |
}); |
| 649 |
|
| 650 |
crc::Crc::<u64, Table<16>>::new(static_algorithm) |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
// Without cache, just leak (no_std without cache feature) |
| 655 |
#[cfg(not(any(feature = "std", feature = "cache")))] |
| 656 |
{ |
| 657 |
let algorithm: Algorithm<u64> = Algorithm { |
| 658 |
width: params.width, |
| 659 |
poly: params.poly, |
| 660 |
init: params.init, |
| 661 |
refin: params.refin, |
| 662 |
refout: params.refout, |
| 663 |
xorout: params.xorout, |
| 664 |
check: params.check, |
| 665 |
residue: 0x0000000000000000, // unused in this context |
| 666 |
}; |
| 667 |
|
| 668 |
// ugly, but the crc crate is difficult to work with... |
| 669 |
let static_algorithm = Box::leak(Box::new(algorithm)); |
| 670 |
|
| 671 |
crc::Crc::<u64, Table<16>>::new(static_algorithm) |
| 672 |
} |
| 673 |
} |
| 674 |
#[cfg(not(feature = "alloc"))] |
| 675 |
panic!("Custom CRC parameters require the 'alloc' feature") |
| 676 |
} |
| 677 |
CrcAlgorithm::CrcCustom => { |
| 678 |
#[cfg(feature = "alloc")] |
| 679 |
{ |
| 680 |
extern crate alloc; |
| 681 |
use alloc::boxed::Box; |
| 682 |
|
| 683 |
#[cfg(any(feature = "std", feature = "cache"))] |
| 684 |
{ |
| 685 |
let key: Crc64Key = ( |
| 686 |
params.poly, |
| 687 |
params.init, |
| 688 |
params.refin, |
| 689 |
params.refout, |
| 690 |
params.xorout, |
| 691 |
params.check, |
| 692 |
); |
| 693 |
|
| 694 |
#[cfg(feature = "std")] |
| 695 |
{ |
| 696 |
let cache = |
| 697 |
CUSTOM_CRC64_CACHE.get_or_init(|| Mutex::new(HashMap::new())); |
| 698 |
let mut cache_guard = cache.lock().unwrap(); |
| 699 |
|
| 700 |
let static_algorithm = |
| 701 |
cache_guard.entry(key).or_insert_with(|| { |
| 702 |
let algorithm: Algorithm<u64> = Algorithm { |
| 703 |
width: params.width, |
| 704 |
poly: params.poly, |
| 705 |
init: params.init, |
| 706 |
refin: params.refin, |
| 707 |
refout: params.refout, |
| 708 |
xorout: params.xorout, |
| 709 |
check: params.check, |
| 710 |
residue: 0x0000000000000000, |
| 711 |
}; |
| 712 |
Box::leak(Box::new(algorithm)) |
| 713 |
}); |
| 714 |
|
| 715 |
crc::Crc::<u64, Table<16>>::new(static_algorithm) |
| 716 |
} |
| 717 |
|
| 718 |
#[cfg(all(feature = "cache", not(feature = "std")))] |
| 719 |
{ |
| 720 |
let cache = |
| 721 |
CUSTOM_CRC64_CACHE.call_once(|| Mutex::new(HashMap::new())); |
| 722 |
let mut cache_guard = cache.lock(); |
| 723 |
|
| 724 |
let static_algorithm = |
| 725 |
cache_guard.entry(key).or_insert_with(|| { |
| 726 |
let algorithm: Algorithm<u64> = Algorithm { |
| 727 |
width: params.width, |
| 728 |
poly: params.poly, |
| 729 |
init: params.init, |
| 730 |
refin: params.refin, |
| 731 |
refout: params.refout, |
| 732 |
xorout: params.xorout, |
| 733 |
check: params.check, |
| 734 |
residue: 0x0000000000000000, |
| 735 |
}; |
| 736 |
Box::leak(Box::new(algorithm)) |
| 737 |
}); |
| 738 |
|
| 739 |
crc::Crc::<u64, Table<16>>::new(static_algorithm) |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
// Without cache, just leak (no_std without cache feature) |
| 744 |
#[cfg(not(any(feature = "std", feature = "cache")))] |
| 745 |
{ |
| 746 |
let algorithm: Algorithm<u64> = Algorithm { |
| 747 |
width: params.width, |
| 748 |
poly: params.poly, |
| 749 |
init: params.init, |
| 750 |
refin: params.refin, |
| 751 |
refout: params.refout, |
| 752 |
xorout: params.xorout, |
| 753 |
check: params.check, |
| 754 |
residue: 0x0000000000000000, |
| 755 |
}; |
| 756 |
|
| 757 |
let static_algorithm = Box::leak(Box::new(algorithm)); |
| 758 |
|
| 759 |
crc::Crc::<u64, Table<16>>::new(static_algorithm) |
| 760 |
} |
| 761 |
} |
| 762 |
#[cfg(not(feature = "alloc"))] |
| 763 |
panic!("Custom CRC parameters require the 'alloc' feature") |
| 764 |
} |
| 765 |
_ => panic!("Invalid algorithm for u64 CRC"), |
| 766 |
}; |
| 767 |
update_u64(state, data, params) |
| 768 |
} |
| 769 |
_ => panic!("Unsupported CRC width: {}", params.width), |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
// Specific implementation for u16 |
| 774 |
fn update_u16(state: u16, data: &[u8], params: crc::Crc<u16, Table<16>>) -> u16 { |
| 775 |
// apply REFIN if necessary |
| 776 |
let initial = if params.algorithm.refin { |
| 777 |
state.reverse_bits() |
| 778 |
} else { |
| 779 |
state |
| 780 |
}; |
| 781 |
|
| 782 |
let mut digest = params.digest_with_initial(initial); |
| 783 |
digest.update(data); |
| 784 |
|
| 785 |
let checksum = digest.finalize(); |
| 786 |
|
| 787 |
// remove XOR since this will be applied in the library Digest::finalize() step instead |
| 788 |
checksum ^ params.algorithm.xorout |
| 789 |
} |
| 790 |
|
| 791 |
// Specific implementation for u32 |
| 792 |
fn update_u32(state: u32, data: &[u8], params: crc::Crc<u32, Table<16>>) -> u32 { |
| 793 |
// apply REFIN if necessary |
| 794 |
let initial = if params.algorithm.refin { |
| 795 |
state.reverse_bits() |
| 796 |
} else { |
| 797 |
state |
| 798 |
}; |
| 799 |
|
| 800 |
let mut digest = params.digest_with_initial(initial); |
| 801 |
digest.update(data); |
| 802 |
|
| 803 |
let checksum = digest.finalize(); |
| 804 |
|
| 805 |
// remove XOR since this will be applied in the library Digest::finalize() step instead |
| 806 |
checksum ^ params.algorithm.xorout |
| 807 |
} |
| 808 |
|
| 809 |
// Specific implementation for u64 |
| 810 |
fn update_u64(state: u64, data: &[u8], params: crc::Crc<u64, Table<16>>) -> u64 { |
| 811 |
// apply REFIN if necessary |
| 812 |
let initial = if params.algorithm.refin { |
| 813 |
state.reverse_bits() |
| 814 |
} else { |
| 815 |
state |
| 816 |
}; |
| 817 |
|
| 818 |
let mut digest = params.digest_with_initial(initial); |
| 819 |
digest.update(data); |
| 820 |
|
| 821 |
// remove XOR since this will be applied in the library Digest::finalize() step instead |
| 822 |
digest.finalize() ^ params.algorithm.xorout |
| 823 |
} |