Add an extra parameter for the xorout value

This change allows to calculate some more variants of the CRC-CCITT
algorithm with a single function.
This commit is contained in:
Jef Driesen 2023-02-10 21:03:39 +01:00
parent 2ba9904757
commit 12f44f3410
12 changed files with 25 additions and 25 deletions

View File

@ -69,7 +69,7 @@ checksum_xor_uint8 (const unsigned char data[], unsigned int size, unsigned char
unsigned short unsigned short
checksum_crc16_ccitt (const unsigned char data[], unsigned int size, unsigned short init) checksum_crc16_ccitt (const unsigned char data[], unsigned int size, unsigned short init, unsigned short xorout)
{ {
static const unsigned short crc_ccitt_table[] = { static const unsigned short crc_ccitt_table[] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
@ -110,7 +110,7 @@ checksum_crc16_ccitt (const unsigned char data[], unsigned int size, unsigned sh
for (unsigned int i = 0; i < size; ++i) for (unsigned int i = 0; i < size; ++i)
crc = (crc << 8) ^ crc_ccitt_table[(crc >> 8) ^ data[i]]; crc = (crc << 8) ^ crc_ccitt_table[(crc >> 8) ^ data[i]];
return crc; return crc ^ xorout;
} }
unsigned int unsigned int

View File

@ -39,7 +39,7 @@ unsigned char
checksum_xor_uint8 (const unsigned char data[], unsigned int size, unsigned char init); checksum_xor_uint8 (const unsigned char data[], unsigned int size, unsigned char init);
unsigned short unsigned short
checksum_crc16_ccitt (const unsigned char data[], unsigned int size, unsigned short init); checksum_crc16_ccitt (const unsigned char data[], unsigned int size, unsigned short init, unsigned short xorout);
unsigned int unsigned int
checksum_crc32 (const unsigned char data[], unsigned int size); checksum_crc32 (const unsigned char data[], unsigned int size);

View File

@ -93,7 +93,7 @@ cressi_goa_device_send (cressi_goa_device_t *device, unsigned char cmd, const un
if (size) { if (size) {
memcpy (packet + 5, data, size); memcpy (packet + 5, data, size);
} }
crc = checksum_crc16_ccitt (packet + 3, size + 2, 0x000); crc = checksum_crc16_ccitt (packet + 3, size + 2, 0x000, 0x0000);
packet[5 + size + 0] = (crc ) & 0xFF; // Low packet[5 + size + 0] = (crc ) & 0xFF; // Low
packet[5 + size + 1] = (crc >> 8) & 0xFF; // High packet[5 + size + 1] = (crc >> 8) & 0xFF; // High
packet[5 + size + 2] = TRAILER; packet[5 + size + 2] = TRAILER;
@ -155,7 +155,7 @@ cressi_goa_device_receive (cressi_goa_device_t *device, unsigned char data[], un
// Verify the checksum of the packet. // Verify the checksum of the packet.
unsigned short crc = array_uint16_le (packet + length + 5); unsigned short crc = array_uint16_le (packet + length + 5);
unsigned short ccrc = checksum_crc16_ccitt (packet + 3, length + 2, 0x0000); unsigned short ccrc = checksum_crc16_ccitt (packet + 3, length + 2, 0x0000, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum."); ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;
@ -203,7 +203,7 @@ cressi_goa_device_download (cressi_goa_device_t *device, dc_buffer_t *buffer, dc
// Verify the checksum of the packet. // Verify the checksum of the packet.
unsigned short crc = array_uint16_le (packet + sizeof(packet) - 2); unsigned short crc = array_uint16_le (packet + sizeof(packet) - 2);
unsigned short ccrc = checksum_crc16_ccitt (packet + 3, sizeof(packet) - 5, 0x0000); unsigned short ccrc = checksum_crc16_ccitt (packet + 3, sizeof(packet) - 5, 0x0000, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum."); ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;

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); array_convert_bin2hex (raw, rsize, ascii + 1, 2 * rsize);
// Checksum // Checksum
unsigned short crc = checksum_crc16_ccitt (ascii + 1, 2 * rsize, 0xffff); unsigned short crc = checksum_crc16_ccitt (ascii + 1, 2 * rsize, 0xffff, 0x0000);
unsigned char checksum[] = { unsigned char checksum[] = {
(crc >> 8) & 0xFF, // High (crc >> 8) & 0xFF, // High
(crc ) & 0xFF}; // Low (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. // Verify the checksum of the packet.
unsigned short crc = array_uint16_be (checksum); unsigned short crc = array_uint16_be (checksum);
unsigned short ccrc = checksum_crc16_ccitt (answer + 1, asize - 6, 0xffff); unsigned short ccrc = checksum_crc16_ccitt (answer + 1, asize - 6, 0xffff, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum."); ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;
@ -372,7 +372,7 @@ cressi_leonardo_device_dump (dc_device_t *abstract, dc_buffer_t *buffer)
// Verify the checksum. // Verify the checksum.
unsigned int csum1 = array_uint16_be (checksum); unsigned int csum1 = array_uint16_be (checksum);
unsigned int csum2 = checksum_crc16_ccitt (data, SZ_MEMORY, 0xffff); unsigned int csum2 = checksum_crc16_ccitt (data, SZ_MEMORY, 0xffff, 0x0000);
if (csum1 != csum2) { if (csum1 != csum2) {
ERROR (abstract->context, "Unexpected answer bytes."); ERROR (abstract->context, "Unexpected answer bytes.");
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;

View File

@ -231,7 +231,7 @@ divesystem_idive_send (divesystem_idive_device_t *device, const unsigned char co
packet[0] = START; packet[0] = START;
packet[1] = csize; packet[1] = csize;
memcpy(packet + 2, command, csize); memcpy(packet + 2, command, csize);
crc = checksum_crc16_ccitt (packet, csize + 2, 0xffff); crc = checksum_crc16_ccitt (packet, csize + 2, 0xffff, 0x0000);
packet[csize + 2] = (crc >> 8) & 0xFF; packet[csize + 2] = (crc >> 8) & 0xFF;
packet[csize + 3] = (crc ) & 0xFF; packet[csize + 3] = (crc ) & 0xFF;
@ -292,7 +292,7 @@ divesystem_idive_receive (divesystem_idive_device_t *device, unsigned char answe
// Verify the checksum. // Verify the checksum.
unsigned short crc = array_uint16_be (packet + len + 2); unsigned short crc = array_uint16_be (packet + len + 2);
unsigned short ccrc = checksum_crc16_ccitt (packet, len + 2, 0xffff); unsigned short ccrc = checksum_crc16_ccitt (packet, len + 2, 0xffff, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected packet checksum."); ERROR (abstract->context, "Unexpected packet checksum.");
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;

View File

@ -158,7 +158,7 @@ liquivision_lynx_recv (liquivision_lynx_device_t *device, unsigned char data[],
// Verify the checksum. // Verify the checksum.
unsigned short crc = array_uint16_be (packet + 1 + size); unsigned short crc = array_uint16_be (packet + 1 + size);
unsigned short ccrc = checksum_crc16_ccitt (packet + 1, size, 0xffff); unsigned short ccrc = checksum_crc16_ccitt (packet + 1, size, 0xffff, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum (%04x %04x).", crc, ccrc); ERROR (abstract->context, "Unexpected answer checksum (%04x %04x).", crc, ccrc);
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;

View File

@ -296,7 +296,7 @@ mares_genius_isvalid (const unsigned char data[], size_t size, unsigned int type
} }
unsigned short crc = array_uint16_le(data + size - 6); unsigned short crc = array_uint16_le(data + size - 6);
unsigned short ccrc = checksum_crc16_ccitt(data + 4, size - 10, 0x0000); unsigned short ccrc = checksum_crc16_ccitt(data + 4, size - 10, 0x0000, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
return 0; return 0;
} }

View File

@ -302,7 +302,7 @@ oceans_s1_xmodem_packet (oceans_s1_device_t *device, unsigned char seq, unsigned
} }
unsigned short crc = array_uint16_be (packet + nbytes - 2); unsigned short crc = array_uint16_be (packet + nbytes - 2);
unsigned short ccrc = checksum_crc16_ccitt (packet + 3, nbytes - 5, 0x0000); unsigned short ccrc = checksum_crc16_ccitt (packet + 3, nbytes - 5, 0x0000, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (device->base.context, "Unexpected answer checksum (%04x %04x).", crc, ccrc); ERROR (device->base.context, "Unexpected answer checksum (%04x %04x).", crc, ccrc);
return DC_STATUS_PROTOCOL; 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. // Verify the checksum of the handshake packet.
unsigned short crc = array_uint16_le (handshake + SZ_HANDSHAKE); unsigned short crc = array_uint16_le (handshake + SZ_HANDSHAKE);
unsigned short ccrc = checksum_crc16_ccitt (handshake, SZ_HANDSHAKE, 0xffff); unsigned short ccrc = checksum_crc16_ccitt (handshake, SZ_HANDSHAKE, 0xffff, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum."); ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL; 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 crc = array_uint16_le (answer + SZ_MEMORY);
unsigned short ccrc = checksum_crc16_ccitt (answer, SZ_MEMORY, 0xffff); unsigned short ccrc = checksum_crc16_ccitt (answer, SZ_MEMORY, 0xffff, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum."); ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL; 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. // Verify the checksum of the packet.
unsigned short crc = array_uint16_le (data + size - 2); unsigned short crc = array_uint16_le (data + size - 2);
unsigned short ccrc = checksum_crc16_ccitt (data + header, size - header - 2, 0xffff); unsigned short ccrc = checksum_crc16_ccitt (data + header, size - header - 2, 0xffff, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected answer checksum."); ERROR (abstract->context, "Unexpected answer checksum.");
return DC_STATUS_PROTOCOL; 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. // Send the checksum to the device.
unsigned short crc = checksum_crc16_ccitt (data, SZ_USER, 0xffff); unsigned short crc = checksum_crc16_ccitt (data, SZ_USER, 0xffff, 0x0000);
rc = reefnet_sensusultra_send_ushort (device, crc); rc = reefnet_sensusultra_send_ushort (device, crc);
if (rc != DC_STATUS_SUCCESS) if (rc != DC_STATUS_SUCCESS)
return rc; return rc;

View File

@ -115,7 +115,7 @@ seac_screen_send (seac_screen_device_t *device, unsigned short cmd, const unsign
if (size) { if (size) {
memcpy (packet + 5, data, size); memcpy (packet + 5, data, size);
} }
crc = checksum_crc16_ccitt (packet, size + 5, 0xFFFF); crc = checksum_crc16_ccitt (packet, size + 5, 0xFFFF, 0x0000);
packet[size + 5] = (crc >> 8) & 0xFF; packet[size + 5] = (crc >> 8) & 0xFF;
packet[size + 6] = (crc ) & 0xFF; packet[size + 6] = (crc ) & 0xFF;
@ -165,7 +165,7 @@ seac_screen_receive (seac_screen_device_t *device, unsigned short cmd, unsigned
// Verify the checksum. // Verify the checksum.
unsigned short crc = array_uint16_be (packet + 1 + length - 2); unsigned short crc = array_uint16_be (packet + 1 + length - 2);
unsigned short ccrc = checksum_crc16_ccitt (packet, 1 + length - 2, 0xFFFF); unsigned short ccrc = checksum_crc16_ccitt (packet, 1 + length - 2, 0xFFFF, 0x0000);
if (crc != ccrc) { if (crc != ccrc) {
ERROR (abstract->context, "Unexpected packet checksum (%04x %04x).", crc, ccrc); ERROR (abstract->context, "Unexpected packet checksum (%04x %04x).", crc, ccrc);
return DC_STATUS_PROTOCOL; return DC_STATUS_PROTOCOL;
@ -486,8 +486,8 @@ seac_screen_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback,
device_event_emit (abstract, DC_EVENT_PROGRESS, &progress); device_event_emit (abstract, DC_EVENT_PROGRESS, &progress);
// Check the header checksums. // Check the header checksums.
if (checksum_crc16_ccitt (logbook[i].header, SZ_HEADER / 2, 0xFFFF) != 0 || if (checksum_crc16_ccitt (logbook[i].header, SZ_HEADER / 2, 0xFFFF, 0x0000) != 0 ||
checksum_crc16_ccitt (logbook[i].header + SZ_HEADER / 2, SZ_HEADER / 2, 0xFFFF) != 0) { checksum_crc16_ccitt (logbook[i].header + SZ_HEADER / 2, SZ_HEADER / 2, 0xFFFF, 0x0000) != 0) {
ERROR (abstract->context, "Unexpected header checksum."); ERROR (abstract->context, "Unexpected header checksum.");
status = DC_STATUS_DATAFORMAT; status = DC_STATUS_DATAFORMAT;
goto error_free_logbook; goto error_free_logbook;

View File

@ -283,8 +283,8 @@ seac_screen_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t
if (abstract->size < SZ_HEADER) if (abstract->size < SZ_HEADER)
return DC_STATUS_DATAFORMAT; return DC_STATUS_DATAFORMAT;
if (checksum_crc16_ccitt (data, SZ_HEADER / 2, 0xFFFF) != 0 || if (checksum_crc16_ccitt (data, SZ_HEADER / 2, 0xFFFF, 0x0000) != 0 ||
checksum_crc16_ccitt (data + SZ_HEADER / 2, SZ_HEADER / 2, 0xFFFF) != 0) { checksum_crc16_ccitt (data + SZ_HEADER / 2, SZ_HEADER / 2, 0xFFFF, 0x0000) != 0) {
ERROR (abstract->context, "Unexpected header checksum."); ERROR (abstract->context, "Unexpected header checksum.");
return DC_STATUS_DATAFORMAT; return DC_STATUS_DATAFORMAT;
} }
@ -303,7 +303,7 @@ seac_screen_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback_t
while (offset + SZ_SAMPLE <= size) { while (offset + SZ_SAMPLE <= size) {
dc_sample_value_t sample = {0}; dc_sample_value_t sample = {0};
if (checksum_crc16_ccitt (data + offset, SZ_SAMPLE, 0xFFFF) != 0) { if (checksum_crc16_ccitt (data + offset, SZ_SAMPLE, 0xFFFF, 0x0000) != 0) {
ERROR (abstract->context, "Unexpected sample checksum."); ERROR (abstract->context, "Unexpected sample checksum.");
return DC_STATUS_DATAFORMAT; return DC_STATUS_DATAFORMAT;
} }