Add an extra parameter for the initial CRC value

This allows to calculate different variants of the CRC-CCITT algorithm
with a single function.
This commit is contained in:
Jef Driesen 2018-11-03 21:17:38 +01:00
parent 4c93e14b0e
commit da2582237f
6 changed files with 12 additions and 12 deletions

View File

@ -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]];

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;