libtinycrc/src/crc16.cpp

23 lines
539 B
C++

#include "tinycrc.h"
/*
CRC16 (from https://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum)
*/
uint16_t tinycrc_crc16(const uint8_t *data, uint16_t size)
{
unsigned char x;
unsigned short crc = 0xFFFF;
if (data == nullptr) {
return 0;
}
for (int index = 0 ; index < size ; ++index) {
x = crc >> 8 ^ *data++;
x ^= x>>4;
crc = (crc << 8) ^ ((unsigned short)(x << 12)) ^ ((unsigned short)(x <<5)) ^ ((unsigned short)x);
}
return crc & 0xFFFF;
}