| 1 |
//! # crc |
| 2 |
//! Rust implementation of CRC. |
| 3 |
//! |
| 4 |
//! ### Examples |
| 5 |
//! Using a well-known algorithm: |
| 6 |
//! ```rust |
| 7 |
//! const X25: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_IBM_SDLC); |
| 8 |
//! assert_eq!(X25.checksum(b"123456789"), 0x906e); |
| 9 |
//! ``` |
| 10 |
//! |
| 11 |
//! Using a custom algorithm: |
| 12 |
//! ```rust |
| 13 |
//! const CUSTOM_ALG: crc::Algorithm<u16> = crc::Algorithm { |
| 14 |
//! width: 16, |
| 15 |
//! poly: 0x8005, |
| 16 |
//! init: 0xffff, |
| 17 |
//! refin: false, |
| 18 |
//! refout: false, |
| 19 |
//! xorout: 0x0000, |
| 20 |
//! check: 0xaee7, |
| 21 |
//! residue: 0x0000 |
| 22 |
//! }; |
| 23 |
//! let crc = crc::Crc::<u16>::new(&CUSTOM_ALG); |
| 24 |
//! let mut digest = crc.digest(); |
| 25 |
//! digest.update(b"123456789"); |
| 26 |
//! assert_eq!(digest.finalize(), 0xaee7); |
| 27 |
//! ``` |
| 28 |
#![no_std] |
| 29 |
#![forbid(unsafe_code)] |
| 30 |
|
| 31 |
pub use crc_catalog::algorithm::*; |
| 32 |
pub use crc_catalog::{Algorithm, Width}; |
| 33 |
|
| 34 |
mod crc128; |
| 35 |
mod crc16; |
| 36 |
mod crc32; |
| 37 |
mod crc64; |
| 38 |
mod crc8; |
| 39 |
mod table; |
| 40 |
mod util; |
| 41 |
|
| 42 |
/// A trait for CRC implementations. |
| 43 |
pub trait Implementation { |
| 44 |
/// Associated data necessary for the implementation (e.g. lookup tables). |
| 45 |
type Data<W>; |
| 46 |
} |
| 47 |
|
| 48 |
/// A table-based implementation of the CRC algorithm, with `L` lanes. |
| 49 |
/// The number of entries in the lookup table is `L * 256`. |
| 50 |
#[derive(Copy, Clone)] |
| 51 |
pub struct Table<const L: usize> {} |
| 52 |
|
| 53 |
/// An implementation of the CRC algorithm with no lookup table. |
| 54 |
pub type NoTable = Table<0>; |
| 55 |
|
| 56 |
type DefaultImpl = Table<1>; |
| 57 |
|
| 58 |
impl<const L: usize> Implementation for Table<L> { |
| 59 |
type Data<W> = [[W; 256]; L]; |
| 60 |
} |
| 61 |
|
| 62 |
mod private { |
| 63 |
pub trait Sealed {} |
| 64 |
impl Sealed for super::Table<0> {} |
| 65 |
impl Sealed for super::Table<1> {} |
| 66 |
impl Sealed for super::Table<16> {} |
| 67 |
} |
| 68 |
|
| 69 |
/// Crc instance with a specific width, algorithm, and implementation. |
| 70 |
#[derive(Clone)] |
| 71 |
pub struct Crc<W: Width, I: Implementation = DefaultImpl> { |
| 72 |
pub algorithm: &'static Algorithm<W>, |
| 73 |
data: I::Data<W>, |
| 74 |
} |
| 75 |
|
| 76 |
#[derive(Clone)] |
| 77 |
pub struct Digest<'a, W: Width, I: Implementation = DefaultImpl> { |
| 78 |
crc: &'a Crc<W, I>, |
| 79 |
value: W, |
| 80 |
} |
| 81 |
|
| 82 |
#[cfg(test)] |
| 83 |
mod test { |
| 84 |
use super::{Crc, CRC_32_ISCSI}; |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn test_clone() { |
| 88 |
const CRC: Crc<u32> = Crc::<u32>::new(&CRC_32_ISCSI); |
| 89 |
let crc = CRC.clone(); |
| 90 |
let digest = crc.digest(); |
| 91 |
let _digest = digest.clone(); |
| 92 |
} |
| 93 |
} |