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