From 5b4660fa633fb8682993db0730d9dc59618b1d45 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Fri, 10 Jun 2022 04:47:10 -0400 Subject: [PATCH] simplify crc code --- common/cmdlib.cc | 26 ++++++++++---------------- include/common/cmdlib.hh | 7 +++---- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/common/cmdlib.cc b/common/cmdlib.cc index 4618bc18..9e74ab6b 100644 --- a/common/cmdlib.cc +++ b/common/cmdlib.cc @@ -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; } diff --git a/include/common/cmdlib.hh b/include/common/cmdlib.hh index 80729ae9..8dd90cf4 100644 --- a/include/common/cmdlib.hh +++ b/include/common/cmdlib.hh @@ -826,7 +826,6 @@ struct is_iterator constexpr bool is_iterator_v = is_iterator::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); \ No newline at end of file +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); \ No newline at end of file