Function crc8_rs::fetch_crc8[][src]

pub fn fetch_crc8<const DATA_SIZE: usize>(
    data: [u8; DATA_SIZE],
    polynomial: u8
) -> u8

Get the current CRC of a data buffer under a generator polynomial.

Calculates the polynomial modulo division of the data buffer with the polynomial. If we give a valid CRC appended data buffer under polynomial, we will get 0 back. The short-hand of this is the has_valid_crc8 function. When given a null terminated data buffer, the fetch_crc8(data, polynomial) ^ polynomial will equal the value needed to be set as the last byte in order to get a valid CRC signed buffer. The short-hand of this is the insert_crc8 function.

Examples

use crc8_rs::{ insert_crc8, has_valid_crc8 };

// We can declare our packets ourselves
struct Packet {
    header: u8,
    content: [u8; 14],
}

impl Packet {
    fn to_bytes(&self) -> [u8; 16] {
        let mut data = [0; 16];

        // Insert the packet data
        data[0] = self.header;
        for i in 0..14 { data[i + 1] = self.content[i] }

        // Insert the CRC at the end of the buffer
        // We use 0xD5 as the generator polynomial here
        insert_crc8(data, 0xD5)
    }
}

let pkt = Packet {
    // ...
};
assert!(has_valid_crc8(pkt.to_bytes(), 0xD5));

Panics

This function will panic when given a zero-sized buffer as can be seen in the following code snippet.

use crc8_rs::fetch_crc8;

fetch_crc8([], 0x42);