2 ** crc.c - calculate CRC
4 ** See Copyright Notice in mruby.h
11 /* Calculate CRC (CRC-16-CCITT)
13 ** 0000_0000_0000_0000_0000_0000_0000_0000
14 ** ^|------- CRC -------|- work --|
17 #define CRC_16_CCITT 0x11021ul /* x^16+x^12+x^5+1 */
18 #define CRC_XOR_PATTERN (CRC_16_CCITT << 8)
19 #define CRC_CARRY_BIT (0x01000000)
22 calc_crc_16_ccitt(const uint8_t *src, size_t nbytes, uint16_t crc)
26 uint32_t crcwk = crc << 8;
28 for (ibyte = 0; ibyte < nbytes; ibyte++) {
30 for (ibit = 0; ibit < CHAR_BIT; ibit++) {
32 if (crcwk & CRC_CARRY_BIT) {
33 crcwk ^= CRC_XOR_PATTERN;
37 return (uint16_t)(crcwk >> 8);