2 * Implementation of Metacube2 utility functions.
4 * Note: This file is meant to compile as both C and C++, for easier inclusion
11 * https://www.ece.cmu.edu/~koopman/pubs/KoopmanCRCWebinar9May2012.pdf
12 * recommends this for messages as short as ours (see table at page 34).
14 #define METACUBE2_CRC_POLYNOMIAL 0x8FDB
16 /* Semi-random starting value to make sure all-zero won't pass. */
17 #define METACUBE2_CRC_START 0x1234
19 /* This code is based on code generated by pycrc. */
20 uint16_t metacube2_compute_crc(const struct metacube2_block_header *hdr)
22 static const int data_len = sizeof(hdr->size) + sizeof(hdr->flags);
23 const uint8_t *data = (uint8_t *)&hdr->size;
24 uint16_t crc = METACUBE2_CRC_START;
27 for (i = 0; i < data_len; ++i) {
29 for (j = 0; j < 8; j++) {
30 int bit = crc & 0x8000;
31 crc = (crc << 1) | ((c >> (7 - j)) & 0x01);
33 crc ^= METACUBE2_CRC_POLYNOMIAL;
39 for (i = 0; i < 16; i++) {
40 int bit = crc & 0x8000;
43 crc ^= METACUBE2_CRC_POLYNOMIAL;