# 12.1. `crc` — Cyclic Redundancy Checks¶

Source code: src/hash/crc.h, src/hash/crc.c

Test code: tst/hash/crc/main.c

Test coverage: src/hash/crc.c

Defines

`CRC_8_POLYNOMIAL_8_5_4_0`

The polynomial `x^8 + x^5 + x^4 + x^0`.

Functions

uint32_t `crc_32`(uint32_t crc, const void *buf_p, size_t size)

Calculate a 32 bits crc using the polynomial `x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0`.

Return
Calculated crc.
Parameters
• `crc`: Initial crc. Often 0x00000000.
• `buf_p`: Buffer to calculate crc of.
• `size`: Size of the buffer.

uint16_t `crc_ccitt`(uint16_t crc, const void *buf_p, size_t size)

Calculate a 16 bits crc using the CCITT algorithm (polynomial `x^16+x^12+x^5+x^1`).

Return
Calculated crc.
Parameters
• `crc`: Initial crc. Should be 0xffff for CCITT.
• `buf_p`: Buffer to calculate crc of.
• `size`: Size of the buffer.

uint16_t `crc_xmodem`(uint16_t crc, const void *buf_p, size_t size)

Calculate a 16 bits crc using the XModem algorithm (polynomial `x^16+x^12+x^5+x^1`).

Return
Calculated crc.
Parameters
• `crc`: Initial crc. Should be 0x0000 for XModem.
• `buf_p`: Buffer to calculate crc of.
• `size`: Size of the buffer.

uint8_t `crc_7`(const void *buf_p, size_t size)

Calculate a 8 bits crc using the CRC-7 algorithm (polynomial `x^7+x^3+1`).

Return
Calculated crc.
Parameters
• `buf_p`: Buffer to calculate crc of.
• `size`: Size of the buffer.

uint8_t `crc_8`(uint8_t crc, uint8_t polynomial, const void *buf_p, size_t size)

Calculate a 8 bits crc using given polynomial.

Return
Calculated crc.
Parameters
• `crc`: Initial crc. Must be 0x00 on first call.
• `polynimial`: CRC polynomial.
• `buf_p`: Buffer to calculate crc of.
• `size`: Size of the buffer.