diff --git a/src/crc16.c b/src/crc16.c index 0177c9da..cee047fa 100644 --- a/src/crc16.c +++ b/src/crc16.c @@ -1,10 +1,10 @@ /* - * Derived from CRC algorithm for JTAG ICE mkII, published in Atmel - * Appnote AVR067. Converted from C++ to C. + * Derived from CRC algorithm for JTAG ICE mkII, published in Atmel Appnote + * AVR067. Converted from C++ to C. */ #include "crc16.h" -/* CRC16 Definitions */ +// CRC16 Definitions static const unsigned short crc_table[256] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, @@ -40,44 +40,31 @@ static const unsigned short crc_table[256] = { 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 }; -/* CRC calculation macros */ +// CRC calculation macros #define CRC_INIT 0xFFFF -#define CRC(crcval,newchar) crcval = (crcval >> 8) ^ \ - crc_table[(crcval ^ newchar) & 0x00ff] +#define CRC(crcval, newchar) ((crcval) = (crcval>>8) ^ crc_table[((crcval) ^ (newchar)) & 0x00ff]) -unsigned short -crcsum(const unsigned char* message, unsigned long length, - unsigned short crc) -{ +unsigned short crcsum(const unsigned char *message, unsigned long length, unsigned short crc) { unsigned long i; - for(i = 0; i < length; i++) - { - CRC(crc, message[i]); - } + for(i = 0; i < length; i++) { + CRC(crc, message[i]); + } return crc; } -int -crcverify(const unsigned char* message, unsigned long length) -{ - /* - * Returns true if the last two bytes in a message is the crc of the - * preceding bytes. - */ +// Returns true if the last two bytes in a message is the crc of the preceding bytes +int crcverify(const unsigned char *message, unsigned long length) { unsigned short expected; expected = crcsum(message, length - 2, CRC_INIT); - return (expected & 0xff) == message[length - 2] && - ((expected >> 8) & 0xff) == message[length - 1]; + return (expected & 0xff) == message[length - 2] && ((expected >> 8) & 0xff) == message[length - 1]; } -void -crcappend(unsigned char* message, unsigned long length) -{ +void crcappend(unsigned char *message, unsigned long length) { unsigned long crc; crc = crcsum(message, length, CRC_INIT); - message[length] = (unsigned char)(crc & 0xff); - message[length+1] = (unsigned char)((crc >> 8) & 0xff); + message[length] = (unsigned char) (crc & 0xff); + message[length + 1] = (unsigned char) ((crc >> 8) & 0xff); }