41#ifndef PROTOCORE_CRC_H
42#define PROTOCORE_CRC_H
61inline uint32_t
mask(uint8_t width)
63 return (width >= 32) ? 0xFFFFFFFFu : ((1u << width) - 1u);
81inline uint8_t
rev8(uint8_t b)
83 b = (uint8_t)(((b & 0xF0u) >> 4) | ((b & 0x0Fu) << 4));
84 b = (uint8_t)(((b & 0xCCu) >> 2) | ((b & 0x33u) << 2));
85 b = (uint8_t)(((b & 0xAAu) >> 1) | ((b & 0x55u) << 1));
90inline uint32_t
revN(uint32_t v, uint8_t width)
93 for (uint8_t i = 0; i < width; i++)
95 r = (r << 1) | (v & 1u);
121 if (!p || (!data && len))
127 const uint32_t top = 1u << (width - 1);
129 for (
size_t i = 0; i < len; i++)
132 crc ^= ((uint32_t)b) << (width - 8);
133 for (
int k = 0; k < 8; k++)
135 crc = (crc & top) ? (((crc << 1) ^ p->
poly) & m) : ((crc << 1) & m);
154 return (crc ^ p->
xorout) & m;
constexpr pc_crc_params PC_CRC16_X25
CRC-16/X-25 (HDLC FCS). check = 0x906E. Used by services/radio/thread, mbplus, nema_ts2.
constexpr pc_crc_params PC_CRC16_ARC
CRC-16/ARC (a.k.a. CRC-16, IBM). check = 0xBB3D.
constexpr pc_crc_params PC_CRC16_DNP
CRC-16/DNP (DNP3 link-layer block check). check = 0xEA82. Used by services/dnp3.
uint32_t pc_crc_final(const pc_crc_params *p, uint32_t crc)
Finish a CRC: apply the output reflection and the final XOR.
constexpr pc_crc_params PC_CRC32_BZIP2
CRC-32/BZIP2 (unreflected CRC-32). check = 0xFC891918.
uint32_t pc_crc_begin(const pc_crc_params *p)
Start a CRC.
constexpr pc_crc_params PC_CRC32_ISO_HDLC
CRC-32/ISO-HDLC (zlib / PKZIP / Ethernet). check = 0xCBF43926.
constexpr pc_crc_params PC_CRC16_KERMIT
CRC-16/KERMIT (a.k.a. CRC-16/CCITT, reflected). check = 0x2189.
constexpr pc_crc_params PC_CRC16_MODBUS
CRC-16/MODBUS. check = 0x4B37.
constexpr pc_crc_params PC_CRC16_XMODEM
CRC-16/XMODEM. check = 0x31C3.
constexpr pc_crc_params PC_CRC8_NRSC5
CRC-8/NRSC-5 - the Sensirion sensor CRC. check = 0xF7. Used by services/sht3x.
constexpr pc_crc_params PC_CRC8_SMBUS
CRC-8/SMBUS (a.k.a. CRC-8). check = 0xF4.
constexpr pc_crc_params PC_CRC16_IBM_3740
CRC-16/IBM-3740 (often called CCITT-FALSE). check = 0x29B1.
constexpr pc_crc_params PC_CRC8_MAXIM_DOW
CRC-8/MAXIM-DOW (1-Wire / Dallas). check = 0xA1.
uint32_t pc_crc(const pc_crc_params *p, const uint8_t *data, size_t len)
One-shot CRC of len octets at data.
uint32_t pc_crc_update(const pc_crc_params *p, uint32_t crc, const uint8_t *data, size_t len)
Fold len octets at data into the running register crc.
constexpr pc_crc_params PC_CRC24_OPENPGP
CRC-24/OPENPGP. check = 0x21CF02.
uint32_t revN(uint32_t v, uint8_t width)
Reverse the low width bits of v.
uint8_t rev8(uint8_t b)
Reverse the 8 bits of b.
uint32_t mask(uint8_t width)
Mask of width low bits (width 32 handled without a 32-bit shift, which is UB).
uint8_t clamp_width(uint8_t width)
Clamp a register width to the supported 8..32 range.
One CRC's full definition (Rocksoft model). See the file comment.
bool refin
reflect each input octet before feeding it in.
uint32_t xorout
XORed into the final register.
uint8_t width
register width in bits, 8..32.
uint32_t poly
generator polynomial, normal form (implicit top bit dropped).
bool refout
reflect the final register before the XOR.
uint32_t init
initial register value.