Function crc8_rs::insert_crc8 [−][src]
pub fn insert_crc8<const DATA_SIZE: usize>(
data: [u8; DATA_SIZE],
polynomial: u8
) -> [u8; DATA_SIZE]
Expand description
Insert CRC byte in the last byte of data
buffer under a generator polynomial
.
This expects a last byte left for the CRC byte, any pre-existing last byte value will be
ignored and overwritten in the return value. This function is very similar to writing
data[data.len() - 1] = polynomial ^ fetch_crc8(data, polynomial)
.
Examples
use crc8_rs::{ has_valid_crc8, insert_crc8 };
const GENERATOR_POLYNOMIAL: u8 = 0xD5;
// We add an empty byte at the end for the CRC
let msg = b"Hello World!\0";
let msg = insert_crc8(*msg, GENERATOR_POLYNOMIAL);
// Will verify just fine!
assert!(has_valid_crc8(msg, GENERATOR_POLYNOMIAL));
let corrupted_msg = {
let mut tmp_msg = msg;
tmp_msg[1] = b'a';
tmp_msg
};
// The message is now corrupted and thus it can't verify the integrity!
assert!(!has_valid_crc8(corrupted_msg, GENERATOR_POLYNOMIAL));
Panics
This function will panic when given a zero-sized buffer as can be seen in the following code snippet.
ⓘ
use crc8_rs::insert_crc8;
insert_crc8([], 0x42);