simplify crc code

This commit is contained in:
Jonathan 2022-06-10 04:47:10 -04:00
parent 42c4060a64
commit 5b4660fa63
2 changed files with 13 additions and 20 deletions

View File

@ -99,10 +99,9 @@ string_iequals(const std::string &a, const std::string &b)
* CCITT standard CRC used by XMODEM
*/
#define CRC_INIT_VALUE 0xffff
#define CRC_XOR_VALUE 0x0000
constexpr uint16_t CRC_INIT_VALUE = 0xffff;
static unsigned short crctable[256] = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129,
static uint16_t crctable[256] = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129,
0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7,
0x44a4, 0x5485, 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630,
@ -122,27 +121,22 @@ static unsigned short crctable[256] = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0
0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0};
void CRC_Init(unsigned short *crcvalue)
void CRC_Init(uint16_t &crcvalue)
{
*crcvalue = CRC_INIT_VALUE;
crcvalue = CRC_INIT_VALUE;
}
void CRC_ProcessByte(unsigned short *crcvalue, uint8_t data)
void CRC_ProcessByte(uint16_t &crcvalue, uint8_t data)
{
*crcvalue = (*crcvalue << 8) ^ crctable[(*crcvalue >> 8) ^ data];
crcvalue = (crcvalue << 8) ^ crctable[(crcvalue >> 8) ^ data];
}
unsigned short CRC_Value(unsigned short crcvalue)
uint16_t CRC_Block(const uint8_t *start, int count)
{
return crcvalue ^ CRC_XOR_VALUE;
}
unsigned short CRC_Block(const unsigned char *start, int count)
{
unsigned short crc;
CRC_Init(&crc);
uint16_t crc;
CRC_Init(crc);
while (count--)
crc = (crc << 8) ^ crctable[(crc >> 8) ^ *start++];
CRC_ProcessByte(crc, *start++);
return crc;
}

View File

@ -826,7 +826,6 @@ struct is_iterator<T,
template<class T>
constexpr bool is_iterator_v = is_iterator<T>::value;
void CRC_Init(unsigned short *crcvalue);
void CRC_ProcessByte(unsigned short *crcvalue, uint8_t data);
unsigned short CRC_Value(unsigned short crcvalue);
unsigned short CRC_Block(const unsigned char *start, int count);
void CRC_Init(uint16_t &crcvalue);
void CRC_ProcessByte(uint16_t &crcvalue, uint8_t data);
uint16_t CRC_Block(const uint8_t *start, int count);