| 1 | //! Crate providing a CRC-24 hasher based on the IETF RFC2440 specification. |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/::Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::hashrust-analyzer cargo std https://github.com/rust-lang/rust/library/std hash/::Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#; |
| 5 | |
| 6 | const INITrust-analyzer cargo crc24 0.1.6 INIT.: u32 = 0xB7_04_CE; |
| 7 | |
| 8 | includerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include!! { concatrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/concat!!(envrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/env!!("OUT_DIR"), "/table.inc") } |
| 9 | |
| 10 | /// CRC-24 hasher based on IETF RFC2440 specification. |
| 11 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:,Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:,PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:,Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)] |
| 12 | pub struct Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { |
| 13 | staterust-analyzer cargo crc24 0.1.6 Crc24Hasher#state.: u32 |
| 14 | } |
| 15 | |
| 16 | impl Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { |
| 17 | /// Creates a new CRC-24 hasher initialized with the given state. |
| 18 | pub fn initrust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher]init().(v: u32) -> Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { |
| 19 | Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { staterust-analyzer cargo crc24 0.1.6 Crc24Hasher#state.: v rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0xFF_FF_FF } |
| 20 | } |
| 21 | /// Creates a new CRC-24 hasher initialized with a nonzero state |
| 22 | /// specified in RFC2440. |
| 23 | pub fn newrust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher]new().() -> Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { |
| 24 | Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { staterust-analyzer cargo crc24 0.1.6 Crc24Hasher#state.: INITrust-analyzer cargo crc24 0.1.6 INIT. } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { |
| 29 | /// Creates a new CRC-24 hasher initialized with a nonzero state |
| 30 | /// specified in RFC2440. |
| 31 | fn defaultrust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher][Default]default().() -> Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { |
| 32 | Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher#::newrust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher]new().() |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher# for Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# { |
| 37 | fn finishrust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher][Hasher]finish().(&self) -> u64 { self.staterust-analyzer cargo crc24 0.1.6 Crc24Hasher#state. as u64 } |
| 38 | |
| 39 | fn writerust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher][Hasher]write().(&mut self, msg: &[u8]) { |
| 40 | let mut s = self.staterust-analyzer cargo crc24 0.1.6 Crc24Hasher#state.; |
| 41 | for &octet in msg.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 42 | let index = ((octet as u32) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXor<Self>`]bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXor<Self>`]bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXor<Self>`]bitxor().(s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().>>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shr<i32>`]shr().16)) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0xFF; |
| 43 | s = (s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().8) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#bitxor().^rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#bitxor(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitXor#bitxor().CRC24_TABLE[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().index as usize]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().; |
| 44 | } |
| 45 | self.staterust-analyzer cargo crc24 0.1.6 Crc24Hasher#state. = s rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0xFF_FF_FF; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// Computes hash of the raw bytes using CRC-24 |
| 50 | /// (without including the length as part of the data) |
| 51 | pub fn hash_rawrust-analyzer cargo crc24 0.1.6 hash_raw().(octets: &[u8]) -> u32 { |
| 52 | let mut h: Crc24Hasherrust-analyzer cargo crc24 0.1.6 Crc24Hasher# = Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher][Default]default().(); |
| 53 | h.writerust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher][Hasher]write().(octets); |
| 54 | h.finishrust-analyzer cargo crc24 0.1.6 impl#[Crc24Hasher][Hasher]finish().() as u32 |
| 55 | } |
| 56 | |
| 57 | #[cfg(test)] |
| 58 | mod testrust-analyzer cargo crc24 0.1.6 test/ { |
| 59 | |
| 60 | const CRC24_INITrust-analyzer cargo crc24 0.1.6 test/CRC24_INIT.: u32 = 0x__b7_04_ce; |
| 61 | const CRC24_POLYrust-analyzer cargo crc24 0.1.6 test/CRC24_POLY.: u32 = 0x1_86_4c_fb; // including x^24 |
| 62 | |
| 63 | // directly translated from RFC2440 section 6.1. |
| 64 | fn crc_octetsrust-analyzer cargo crc24 0.1.6 test/crc_octets().(octets: &[u8]) -> u32 { |
| 65 | let mut crc = CRC24_INITrust-analyzer cargo crc24 0.1.6 test/CRC24_INIT.; |
| 66 | for &octet in octets.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() { |
| 67 | crc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXorAssign<Self>`]bitxor_assign().^=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXorAssign<Self>`]bitxor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXorAssign<Self>`]bitxor_assign().(octet as u32) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<i32>`]shl().16; |
| 68 | for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#8 { |
| 69 | crc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShlAssign<i32>`]shl_assign().<<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShlAssign<i32>`]shl_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`ShlAssign<i32>`]shl_assign().1; |
| 70 | if (crc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0x1_00_00_00) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0 { |
| 71 | crc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXorAssign<Self>`]bitxor_assign().^=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXorAssign<Self>`]bitxor_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitXorAssign<Self>`]bitxor_assign().CRC24_POLYrust-analyzer cargo crc24 0.1.6 test/CRC24_POLY.; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | crc rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0xFF_FF_FF |
| 76 | } |
| 77 | |
| 78 | fn test_compare_implsrust-analyzer cargo crc24 0.1.6 test/test_compare_impls().(octets: &[u8]) -> bool { |
| 79 | let h1 = crc_octetsrust-analyzer cargo crc24 0.1.6 test/crc_octets().(octets); |
| 80 | let h2 = superrust-analyzer cargo crc24 0.1.6 crate/::hash_rawrust-analyzer cargo crc24 0.1.6 hash_raw().(octets); |
| 81 | h1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().h2 |
| 82 | } |
| 83 | |
| 84 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 85 | fn testrust-analyzer cargo crc24 0.1.6 test/test().() { |
| 86 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(test_compare_implsrust-analyzer cargo crc24 0.1.6 test/test_compare_impls().(b"")); |
| 87 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(test_compare_implsrust-analyzer cargo crc24 0.1.6 test/test_compare_impls().(b"x")); |
| 88 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(test_compare_implsrust-analyzer cargo crc24 0.1.6 test/test_compare_impls().(b"sg")); |
| 89 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(test_compare_implsrust-analyzer cargo crc24 0.1.6 test/test_compare_impls().(b"crc")); |
| 90 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(test_compare_implsrust-analyzer cargo crc24 0.1.6 test/test_compare_impls().(b"test")); |
| 91 | } |
| 92 | |
| 93 | } // mod test |
| 94 |