Skip to main content

crc_catalog/
lib.rs

1#![no_std]
2#![forbid(unsafe_code)]
3
4#[rustfmt::skip]
5pub mod algorithm;
6pub mod poly;
7pub use algorithm::*;
8
9pub trait Width: Sized + 'static {}
10impl Width for u8 {}
11impl Width for u16 {}
12impl Width for u32 {}
13impl Width for u64 {}
14impl Width for u128 {}
15
16/// This struct describes a CRC algorithm using the fields specified by the [Catalogue of
17/// parametrised CRC algorithms](https://reveng.sourceforge.io/crc-catalogue/all.htm).
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct Algorithm<W: Width> {
20    /// The number of bit cells in the linear feedback shift register; the degree of the generator
21    /// polynomial, minus one.
22    pub width: u8,
23    /// The generator polynomial that sets the feedback tap positions of the shift register. The
24    /// least significant bit corresponds to the inward end of the shift register, and is always
25    /// set. The highest-order term is omitted.
26    pub poly: W,
27    /// The settings of the bit cells at the start of each calculation, before reading the first
28    /// message bit. The least significant bit corresponds to the inward end of the shift register.
29    pub init: W,
30    /// If equal to `false`, specifies that the characters of the message are read bit-by-bit, most
31    /// significant bit (MSB) first; if equal to `true`, the characters are read bit-by-bit, least
32    /// significant bit (LSB) first. Each sampled message bit is then XORed with the bit being
33    /// simultaneously shifted out of the register at the most significant end, and the result is
34    /// passed to the feedback taps.
35    pub refin: bool,
36    /// If equal to `false`, specifies that the contents of the register after reading the last
37    /// message bit are unreflected before presentation; if equal to `true`, it specifies that they
38    /// are reflected, character-by-character, before presentation. For the purpose of this
39    /// definition, the reflection is performed by swapping the content of each cell with that of
40    /// the cell an equal distance from the opposite end of the register; the characters of the CRC
41    /// are then true images of parts of the reflected register, the character containing the
42    /// original MSB always appearing first.
43    pub refout: bool,
44    /// The XOR value applied to the contents of the register after the last message bit has been
45    /// read and after the optional reflection. It has the same endianness as the CRC such that its
46    /// true image appears in the characters of the CRC.
47    pub xorout: W,
48    /// The contents of the register after initialising, reading the UTF-8 string `"123456789"` (as
49    /// 8-bit characters), optionally reflecting, and applying the final XOR.
50    pub check: W,
51    /// The contents of the register after initialising, reading an error-free codeword and
52    /// optionally reflecting the register (if [`refout`](Algorithm::refout)=`true`), but not
53    /// applying the final XOR. This is mathematically equivalent to initialising the register with
54    /// the xorout parameter, reflecting it as described (if [`refout`](Algorithm::refout)=`true`),
55    /// reading as many zero bits as there are cells in the register, and reflecting the result (if
56    /// [`refin`](Algorithm::refin)=`true`). The residue of a crossed-endian model is calculated
57    /// assuming that the characters of the received CRC are specially reflected before submitting
58    /// the codeword.
59    pub residue: W,
60}
61
62#[cfg(test)]
63#[rustfmt::skip]
64mod tests;