acoustic_ofdm/
crc.rs

1// Copyright (c) 2026 Elias S. G. Carotti
2
3/// Computes CRC-16/CCITT-FALSE over `data`.
4///
5/// Parameters:
6/// - `data`: input bytes.
7/// Returns:
8/// - `u16`: CRC value.
9pub fn crc16_ccitt(data: &[u8]) -> u16 {
10    let mut crc: u16 = 0xFFFF;
11    for &byte in data {
12        crc ^= (byte as u16) << 8;
13        for _ in 0..8 {
14            if (crc & 0x8000) != 0 {
15                crc = (crc << 1) ^ 0x1021;
16            } else {
17                crc <<= 1;
18            }
19        }
20    }
21    crc
22}
23
24#[cfg(test)]
25mod tests {
26    use super::crc16_ccitt;
27
28    #[test]
29    /// Verifies the standard CRC test vector.
30    fn crc16_ccitt_standard_vector() {
31        // CRC-16/CCITT-FALSE test vector.
32        assert_eq!(crc16_ccitt(b"123456789"), 0x29B1);
33    }
34
35    #[test]
36    /// Verifies CRC for an empty payload.
37    fn crc16_ccitt_empty_payload() {
38        assert_eq!(crc16_ccitt(&[]), 0xFFFF);
39    }
40}
41
42// vim: set ts=4 sw=4 et: