Module crc

Source
Available on crate feature unstable only.
Expand description

§Cyclic Redundancy Check (CRC)

§Overview

These are safe abstractions to the CRC functions in the ESP32 ROM. Some chips may not include all of these functions so they will be compiled into the program binary in those cases.

§Configuration

The ROM provides the following polynomials for each CRC width:

CRC WidthPolynomial
CRC-80x07
CRC-160x1021
CRC-320x04c11db7

The “big-endian” *_be() functions are left-shifting algorithms to be used when input and output reflection are not needed. If input and output reflection are needed, the right-shifting “little-endian” *_le() functions should be used.

These functions are designed to compute a CRC over a single buffer or as an ongoing calculation over multiple buffers. To do this, the initial value passed in and the final value returned are one’s complemented.

// CRC-32/MPEG-2
const CRC_INITIAL: u32 = 0xffffffff; // "init" or "xorin" of all ones
let mut crc = crc32_be(!CRC_INITIAL, &data0.as_ref()); // start
crc = crc32_be(crc, &data1.as_ref());
crc = !crc32_be(crc, &data2.as_ref()); // finish

§Examples

A catalogue of these parameters can be found at https://reveng.sourceforge.io/crc-catalogue/all.htm

§CRC-32/ISO-HDLC poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0xffffffff

let crc = crc32_le(!0xffffffff, &data.as_ref());

§CRC-32/BZIP2 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0xffffffff

let crc = crc32_be(!0xffffffff, &data.as_ref());

§CRC-32/MPEG-2 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0x00000000

let crc = !crc32_be(!0xffffffff, &data.as_ref());

§CRC-32/CKSUM poly=0x04c11db7 init=0x00000000 refin=false refout=false xorout=0xffffffff

let crc = crc32_be(!0, &data.as_ref());

§CRC-16/KERMIT poly=0x1021 init=0x0000 refin=true refout=true xorout=0x0000

let crc = !crc16_le(!0, &data.as_ref());

Functions§

crc8_be
Left-shifting CRC-8 with polynomial 0x07
crc8_le
Right-shifting CRC-8 with polynomial 0x07
crc16_be
Left-shifting CRC-16 with polynomial 0x1021
crc16_le
Right-shifting CRC-16 with polynomial 0x1021
crc32_be
Left-shifting CRC-32 with polynomial 0x04c11db7
crc32_le
Right-shifting CRC-32 with polynomial 0x04c11db7