blob: 4ed307b3040a5b8374a5e4dbad46a83cedcee58c [file] [log] [blame]
#include "crc.hpp"
/*
* This implementation tracks the specification given at
* http://srecord.sourceforge.net/crc16-ccitt.html
* Code copied from internal portable sources.
*/
std::uint16_t generateCrc(const std::vector<std::uint8_t>& data)
{
const std::uint16_t kPoly = 0x1021;
const std::uint16_t kLeftBit = 0x8000;
const int kExtraRounds = 2;
const std::uint8_t* bytes = data.data();
std::uint16_t crc = 0xFFFF;
size_t i;
size_t j;
size_t size = data.size();
for (i = 0; i < size + kExtraRounds; ++i)
{
for (j = 0; j < 8; ++j)
{
bool xor_flag = (crc & kLeftBit) ? 1 : 0;
crc <<= 1;
// If this isn't an extra round and the current byte's j'th bit from
// the left is set, increment the CRC.
if (i < size && (bytes[i] & (1 << (7 - j))))
{
crc++;
}
if (xor_flag)
{
crc ^= kPoly;
}
}
}
return crc;
}