rtcm3 0.1.0

no_std, no_alloc parser and encoder for RTCM3 GNSS RTK correction messages
Documentation
//! RTCM3's CRC-24Q checksum (the Qualcomm CRC-24 variant, also used by
//! `FlexRay` and others), computed over the preamble, length field, and
//! payload of a frame.
//!
//! Ported byte-for-byte from `pyrtcm`'s own `calc_crc24q`: a bit-shift
//! accumulator, no lookup table needed.

const POLY: u32 = 0x0186_4CFB;

/// Computes the CRC-24Q checksum over `data` (for a full frame, everything
/// from the preamble through the end of the payload).
#[must_use]
pub fn checksum(data: &[u8]) -> u32 {
    let mut crc: u32 = 0;
    for &byte in data {
        crc ^= u32::from(byte) << 16;
        for _ in 0..8 {
            crc <<= 1;
            if crc & 0x0100_0000 != 0 {
                crc ^= POLY;
            }
        }
    }
    crc & 0x00FF_FFFF
}

// Even though `data` is a fixed-size array at this call site, checksum()'s
// parameter type is the general &[u8], and CBMC does not infer a concrete
// unwind bound for the *outer* of the two nested loops here on its own
// (unlike a single flat loop over a slice, which it does handle without an
// explicit bound) - without #[kani::unwind], this ran past 450 iterations
// with no result. The inner `0..8` loop is a fixed literal range and needs
// no bound of its own.
#[cfg(kani)]
mod kani_proofs {
    use super::checksum;

    #[kani::proof]
    #[kani::unwind(17)]
    fn checksum_never_panics() {
        let data: [u8; 16] = kani::any();
        let _ = checksum(&data);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn matches_pyrtcm_on_an_arbitrary_buffer() {
        // pyrtcm.calc_crc24q(bytes([1,2,3,4,5]))
        assert_eq!(checksum(&[1, 2, 3, 4, 5]), 0x00BF_616B);
    }

    #[test]
    fn empty_input_is_zero() {
        assert_eq!(checksum(&[]), 0);
    }

    #[test]
    fn matches_a_real_captured_mt1230_frame() {
        // Real frame from pyrtcm's own test corpus (pygpsdata-1230.bin):
        // preamble+length+payload should checksum to the trailing 3 bytes.
        let frame = [0xd3, 0x00, 0x04, 0x4c, 0xe0, 0x00, 0x80, 0xed, 0xed, 0xd6];
        let (body, crc_bytes) = frame.split_at(frame.len() - 3);
        let expected = (u32::from(crc_bytes[0]) << 16)
            | (u32::from(crc_bytes[1]) << 8)
            | u32::from(crc_bytes[2]);
        assert_eq!(checksum(body), expected);
    }
}