From da2582237f7ae35c783690cbb3848022545ab97b Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 3 Nov 2018 21:17:38 +0100 Subject: [PATCH] Add an extra parameter for the initial CRC value This allows to calculate different variants of the CRC-CCITT algorithm with a single function. --- src/checksum.c | 4 ++-- src/checksum.h | 2 +- src/cressi_leonardo.c | 6 +++--- src/divesystem_idive.c | 4 ++-- src/reefnet_sensuspro.c | 4 ++-- src/reefnet_sensusultra.c | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/checksum.c b/src/checksum.c index a8b332e..816c58d 100644 --- a/src/checksum.c +++ b/src/checksum.c @@ -69,7 +69,7 @@ checksum_xor_uint8 (const unsigned char data[], unsigned int size, unsigned char unsigned short -checksum_crc_ccitt_uint16 (const unsigned char data[], unsigned int size) +checksum_crc16_ccitt (const unsigned char data[], unsigned int size, unsigned short init) { static const unsigned short crc_ccitt_table[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, @@ -106,7 +106,7 @@ checksum_crc_ccitt_uint16 (const unsigned char data[], unsigned int size) 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 }; - unsigned short crc = 0xffff; + unsigned short crc = init; for (unsigned int i = 0; i < size; ++i) crc = (crc << 8) ^ crc_ccitt_table[(crc >> 8) ^ data[i]]; diff --git a/src/checksum.h b/src/checksum.h index a6ead09..9a53aa6 100644 --- a/src/checksum.h +++ b/src/checksum.h @@ -39,7 +39,7 @@ unsigned char checksum_xor_uint8 (const unsigned char data[], unsigned int size, unsigned char init); unsigned short -checksum_crc_ccitt_uint16 (const unsigned char data[], unsigned int size); +checksum_crc16_ccitt (const unsigned char data[], unsigned int size, unsigned short init); unsigned int checksum_crc32 (const unsigned char data[], unsigned int size); diff --git a/src/cressi_leonardo.c b/src/cressi_leonardo.c index 5359f66..244b887 100644 --- a/src/cressi_leonardo.c +++ b/src/cressi_leonardo.c @@ -84,7 +84,7 @@ cressi_leonardo_make_ascii (const unsigned char raw[], unsigned int rsize, unsig array_convert_bin2hex (raw, rsize, ascii + 1, 2 * rsize); // Checksum - unsigned short crc = checksum_crc_ccitt_uint16 (ascii + 1, 2 * rsize); + unsigned short crc = checksum_crc16_ccitt (ascii + 1, 2 * rsize, 0xffff); unsigned char checksum[] = { (crc >> 8) & 0xFF, // High (crc ) & 0xFF}; // Low @@ -129,7 +129,7 @@ cressi_leonardo_packet (cressi_leonardo_device_t *device, const unsigned char co // Verify the checksum of the packet. unsigned short crc = array_uint16_be (checksum); - unsigned short ccrc = checksum_crc_ccitt_uint16 (answer + 1, asize - 6); + unsigned short ccrc = checksum_crc16_ccitt (answer + 1, asize - 6, 0xffff); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; @@ -372,7 +372,7 @@ cressi_leonardo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) // Verify the checksum. unsigned int csum1 = array_uint16_be (checksum); - unsigned int csum2 = checksum_crc_ccitt_uint16 (data, SZ_MEMORY); + unsigned int csum2 = checksum_crc16_ccitt (data, SZ_MEMORY, 0xffff); if (csum1 != csum2) { ERROR (abstract->context, "Unexpected answer bytes."); return DC_STATUS_PROTOCOL; diff --git a/src/divesystem_idive.c b/src/divesystem_idive.c index 790e71a..f827b52 100644 --- a/src/divesystem_idive.c +++ b/src/divesystem_idive.c @@ -196,7 +196,7 @@ divesystem_idive_send (divesystem_idive_device_t *device, const unsigned char co packet[0] = START; packet[1] = csize; memcpy(packet + 2, command, csize); - crc = checksum_crc_ccitt_uint16 (packet, csize + 2); + crc = checksum_crc16_ccitt (packet, csize + 2, 0xffff); packet[csize + 2] = (crc >> 8) & 0xFF; packet[csize + 3] = (crc ) & 0xFF; @@ -257,7 +257,7 @@ divesystem_idive_receive (divesystem_idive_device_t *device, unsigned char answe // Verify the checksum. unsigned short crc = array_uint16_be (packet + len + 2); - unsigned short ccrc = checksum_crc_ccitt_uint16 (packet, len + 2); + unsigned short ccrc = checksum_crc16_ccitt (packet, len + 2, 0xffff); if (crc != ccrc) { ERROR (abstract->context, "Unexpected packet checksum."); return DC_STATUS_PROTOCOL; diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c index b33fe9a..835fbcb 100644 --- a/src/reefnet_sensuspro.c +++ b/src/reefnet_sensuspro.c @@ -177,7 +177,7 @@ reefnet_sensuspro_handshake (reefnet_sensuspro_device_t *device) // Verify the checksum of the handshake packet. unsigned short crc = array_uint16_le (handshake + SZ_HANDSHAKE); - unsigned short ccrc = checksum_crc_ccitt_uint16 (handshake, SZ_HANDSHAKE); + unsigned short ccrc = checksum_crc16_ccitt (handshake, SZ_HANDSHAKE, 0xffff); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; @@ -280,7 +280,7 @@ reefnet_sensuspro_device_dump (dc_device_t *abstract, dc_buffer_t *buffer) } unsigned short crc = array_uint16_le (answer + SZ_MEMORY); - unsigned short ccrc = checksum_crc_ccitt_uint16 (answer, SZ_MEMORY); + unsigned short ccrc = checksum_crc16_ccitt (answer, SZ_MEMORY, 0xffff); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; diff --git a/src/reefnet_sensusultra.c b/src/reefnet_sensusultra.c index 22ffe81..6b059d6 100644 --- a/src/reefnet_sensusultra.c +++ b/src/reefnet_sensusultra.c @@ -224,7 +224,7 @@ reefnet_sensusultra_packet (reefnet_sensusultra_device_t *device, unsigned char // Verify the checksum of the packet. unsigned short crc = array_uint16_le (data + size - 2); - unsigned short ccrc = checksum_crc_ccitt_uint16 (data + header, size - header - 2); + unsigned short ccrc = checksum_crc16_ccitt (data + header, size - header - 2, 0xffff); if (crc != ccrc) { ERROR (abstract->context, "Unexpected answer checksum."); return DC_STATUS_PROTOCOL; @@ -477,7 +477,7 @@ reefnet_sensusultra_device_write_user (dc_device_t *abstract, const unsigned cha } // Send the checksum to the device. - unsigned short crc = checksum_crc_ccitt_uint16 (data, SZ_USER); + unsigned short crc = checksum_crc16_ccitt (data, SZ_USER, 0xffff); rc = reefnet_sensusultra_send_ushort (device, crc); if (rc != DC_STATUS_SUCCESS) return rc;