]> code.octet-stream.net Git - m17rt/blob - m17core/src/crc.rs
Soundmodem rx works in realtime end-to-end using file source
[m17rt] / m17core / src / crc.rs
1 pub const M17_ALG: crc::Algorithm<u16> = crc::Algorithm {
2 width: 16,
3 poly: 0x5935,
4 init: 0xFFFF,
5 refin: false,
6 refout: false,
7 xorout: 0x0000,
8 check: 0x772B,
9 residue: 0x0000,
10 };
11
12 pub fn m17_crc(input: &[u8]) -> u16 {
13 let crc = crc::Crc::<u16>::new(&M17_ALG);
14 let mut digest = crc.digest();
15 digest.update(input);
16 digest.finalize()
17 }
18
19 #[cfg(test)]
20 mod tests {
21 use super::*;
22
23 #[test]
24 fn crc_test_vectors() {
25 assert_eq!(m17_crc(&[]), 0xFFFF);
26 assert_eq!(m17_crc("A".as_bytes()), 0x206E);
27 assert_eq!(m17_crc("123456789".as_bytes()), 0x772B);
28 let bytes: Vec<u8> = (0x00..=0xFF).collect();
29 assert_eq!(m17_crc(&bytes), 0x1C31);
30 }
31 }