2015-04-11 10:38:37 -07:00
|
|
|
enum { CRC_TABLE_SIZE = 0x100 };
|
2012-08-04 10:03:16 -07:00
|
|
|
|
2015-04-11 10:48:23 -07:00
|
|
|
static inline uint32_t
|
2015-04-11 10:41:52 -07:00
|
|
|
crc_reflect(uint32_t input)
|
2012-08-04 10:03:16 -07:00
|
|
|
{
|
2015-04-11 10:41:52 -07:00
|
|
|
uint32_t reflected = 0;
|
2017-04-24 11:11:42 -07:00
|
|
|
for (int i = 0; i < 32; i++) {
|
2012-08-04 10:03:16 -07:00
|
|
|
reflected <<= 1;
|
|
|
|
reflected |= input & 1;
|
|
|
|
input >>= 1;
|
|
|
|
}
|
|
|
|
return reflected;
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:48:23 -07:00
|
|
|
static inline void
|
2015-04-11 11:04:03 -07:00
|
|
|
crc_be_fill_table(uint32_t *table, uint32_t polynomial)
|
2012-08-04 10:03:16 -07:00
|
|
|
{
|
2015-04-11 11:04:03 -07:00
|
|
|
const uint32_t lsb = 1 << 31;
|
|
|
|
uint32_t poly = polynomial;
|
2012-08-04 10:03:16 -07:00
|
|
|
|
2017-04-24 11:11:42 -07:00
|
|
|
for (int c = 0; c < CRC_TABLE_SIZE; c++) {
|
2015-04-11 11:04:03 -07:00
|
|
|
uint32_t v = c << 24;
|
2015-04-11 10:34:48 -07:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2015-04-11 11:04:03 -07:00
|
|
|
if (v & lsb) {
|
|
|
|
v <<= 1;
|
|
|
|
v ^= poly;
|
2012-08-04 10:03:16 -07:00
|
|
|
} else {
|
2015-04-11 11:04:03 -07:00
|
|
|
v <<= 1;
|
2012-08-04 10:03:16 -07:00
|
|
|
}
|
|
|
|
}
|
2017-04-24 11:11:42 -07:00
|
|
|
*table++ = v;
|
2015-04-11 11:04:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
crc_le_fill_table(uint32_t *table, uint32_t polynomial)
|
|
|
|
{
|
|
|
|
const uint32_t lsb = 1;
|
|
|
|
uint32_t poly = crc_reflect(polynomial);
|
|
|
|
|
2017-04-24 11:11:42 -07:00
|
|
|
for (int c = 0; c < CRC_TABLE_SIZE; c++) {
|
2015-04-11 11:04:03 -07:00
|
|
|
uint32_t v = c;
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
if (v & lsb) {
|
|
|
|
v >>= 1;
|
|
|
|
v ^= poly;
|
|
|
|
} else {
|
|
|
|
v >>= 1;
|
|
|
|
}
|
|
|
|
}
|
2017-04-24 11:11:42 -07:00
|
|
|
*table++ = v;
|
2012-08-04 10:03:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:48:23 -07:00
|
|
|
static inline void
|
2015-04-11 11:10:53 -07:00
|
|
|
crc_be_cycle(uint32_t *table, uint32_t *remainder, uint8_t c)
|
2012-08-04 10:03:16 -07:00
|
|
|
{
|
2017-04-24 11:11:42 -07:00
|
|
|
const uint8_t i = (*remainder >> 24) ^ c;
|
|
|
|
*remainder = (*remainder << 8) ^ table[i];
|
2012-08-04 10:03:16 -07:00
|
|
|
}
|
|
|
|
|
2015-04-11 10:48:23 -07:00
|
|
|
static inline void
|
2015-04-11 11:10:53 -07:00
|
|
|
crc_le_cycle(uint32_t *table, uint32_t *remainder, uint8_t c)
|
2012-08-04 10:03:16 -07:00
|
|
|
{
|
2017-04-24 11:11:42 -07:00
|
|
|
const uint8_t i = (*remainder & 0xFF) ^ c;
|
|
|
|
*remainder = (*remainder >> 8) ^ table[i];
|
2012-08-04 10:03:16 -07:00
|
|
|
}
|