Skip to content

Commit

Permalink
Switch CRC-CCITT libraries (apache#6499)
Browse files Browse the repository at this point in the history
  • Loading branch information
areusch authored and trevor-m committed Sep 18, 2020
1 parent 739aa26 commit ae9a3da
Show file tree
Hide file tree
Showing 14 changed files with 570 additions and 148 deletions.
21 changes: 21 additions & 0 deletions 3rdparty/libcrc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*~
.*.swp
lib/*.lib
lib/*.a
obj/*.obj
obj/*.o
test/obj/*.obj
test/obj/*.o
examples/obj/*.obj
examples/obj/*.o
precalc/obj/*.obj
precalc/obj/*.o
tab/gentab32.inc
tab/gentab64.inc
make.exe
testall.exe
testall
tstcrc.exe
tstcrc
bin/prc.exe
bin/prc
116 changes: 116 additions & 0 deletions 3rdparty/libcrc/include/checksum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Library: libcrc
* File: include/checksum.h
* Author: Lammert Bies
*
* This file is licensed under the MIT License as stated below
*
* Copyright (c) 1999-2018 Lammert Bies
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Description
* -----------
* The headerfile include/checksum.h contains the definitions and prototypes
* for routines that can be used to calculate several kinds of checksums.
*/

#ifndef DEF_LIBCRC_CHECKSUM_H
#define DEF_LIBCRC_CHECKSUM_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <stdint.h>

/*
* #define CRC_POLY_xxxx
*
* The constants of the form CRC_POLY_xxxx define the polynomials for some well
* known CRC calculations.
*/

#define CRC_POLY_16 0xA001
#define CRC_POLY_32 0xEDB88320ul
#define CRC_POLY_64 0x42F0E1EBA9EA3693ull
#define CRC_POLY_CCITT 0x1021
#define CRC_POLY_DNP 0xA6BC
#define CRC_POLY_KERMIT 0x8408
#define CRC_POLY_SICK 0x8005

/*
* #define CRC_START_xxxx
*
* The constants of the form CRC_START_xxxx define the values that are used for
* initialization of a CRC value for common used calculation methods.
*/

#define CRC_START_8 0x00
#define CRC_START_16 0x0000
#define CRC_START_MODBUS 0xFFFF
#define CRC_START_XMODEM 0x0000
#define CRC_START_CCITT_1D0F 0x1D0F
#define CRC_START_CCITT_FFFF 0xFFFF
#define CRC_START_KERMIT 0x0000
#define CRC_START_SICK 0x0000
#define CRC_START_DNP 0x0000
#define CRC_START_32 0xFFFFFFFFul
#define CRC_START_64_ECMA 0x0000000000000000ull
#define CRC_START_64_WE 0xFFFFFFFFFFFFFFFFull

/*
* Prototype list of global functions
*/

unsigned char* checksum_NMEA(const unsigned char* input_str, unsigned char* result);
uint8_t crc_8(const unsigned char* input_str, size_t num_bytes);
uint16_t crc_16(const unsigned char* input_str, size_t num_bytes);
uint32_t crc_32(const unsigned char* input_str, size_t num_bytes);
uint64_t crc_64_ecma(const unsigned char* input_str, size_t num_bytes);
uint64_t crc_64_we(const unsigned char* input_str, size_t num_bytes);
uint16_t crc_ccitt_1d0f(const unsigned char* input_str, size_t num_bytes);
uint16_t crc_ccitt_ffff(const unsigned char* input_str, size_t num_bytes);
uint16_t crc_dnp(const unsigned char* input_str, size_t num_bytes);
uint16_t crc_kermit(const unsigned char* input_str, size_t num_bytes);
uint16_t crc_modbus(const unsigned char* input_str, size_t num_bytes);
uint16_t crc_sick(const unsigned char* input_str, size_t num_bytes);
uint16_t crc_xmodem(const unsigned char* input_str, size_t num_bytes);
uint8_t update_crc_8(uint8_t crc, unsigned char c);
uint16_t update_crc_16(uint16_t crc, unsigned char c);
uint32_t update_crc_32(uint32_t crc, unsigned char c);
uint64_t update_crc_64_ecma(uint64_t crc, unsigned char c);
uint16_t update_crc_ccitt(uint16_t crc, unsigned char c);
uint16_t update_crc_dnp(uint16_t crc, unsigned char c);
uint16_t update_crc_kermit(uint16_t crc, unsigned char c);
uint16_t update_crc_sick(uint16_t crc, unsigned char c, unsigned char prev_byte);

/*
* Global CRC lookup tables
*/

extern const uint32_t crc_tab32[];
extern const uint64_t crc_tab64[];

#ifdef __cplusplus
} // extern "C"
#endif

#endif // DEF_LIBCRC_CHECKSUM_H
119 changes: 119 additions & 0 deletions 3rdparty/libcrc/src/crcccitt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Library: libcrc
* File: src/crcccitt.c
* Author: Lammert Bies
*
* This file is licensed under the MIT License as stated below
*
* Copyright (c) 1999-2016 Lammert Bies
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Description
* -----------
* The module src/crcccitt.c contains routines which are used to calculate the
* CCITT CRC values of a string of bytes.
*/

#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>

#include "../tab/gentab_ccitt.inc"
#include "checksum.h"

static uint16_t crc_ccitt_generic(const unsigned char* input_str, size_t num_bytes,
uint16_t start_value);

/*
* uint16_t crc_xmodem( const unsigned char *input_str, size_t num_bytes );
*
* The function crc_xmodem() performs a one-pass calculation of an X-Modem CRC
* for a byte string that has been passed as a parameter.
*/

uint16_t crc_xmodem(const unsigned char* input_str, size_t num_bytes) {
return crc_ccitt_generic(input_str, num_bytes, CRC_START_XMODEM);

} /* crc_xmodem */

/*
* uint16_t crc_ccitt_1d0f( const unsigned char *input_str, size_t num_bytes );
*
* The function crc_ccitt_1d0f() performs a one-pass calculation of the CCITT
* CRC for a byte string that has been passed as a parameter. The initial value
* 0x1d0f is used for the CRC.
*/

uint16_t crc_ccitt_1d0f(const unsigned char* input_str, size_t num_bytes) {
return crc_ccitt_generic(input_str, num_bytes, CRC_START_CCITT_1D0F);

} /* crc_ccitt_1d0f */

/*
* uint16_t crc_ccitt_ffff( const unsigned char *input_str, size_t num_bytes );
*
* The function crc_ccitt_ffff() performs a one-pass calculation of the CCITT
* CRC for a byte string that has been passed as a parameter. The initial value
* 0xffff is used for the CRC.
*/

uint16_t crc_ccitt_ffff(const unsigned char* input_str, size_t num_bytes) {
return crc_ccitt_generic(input_str, num_bytes, CRC_START_CCITT_FFFF);

} /* crc_ccitt_ffff */

/*
* static uint16_t crc_ccitt_generic( const unsigned char *input_str, size_t num_bytes, uint16_t
* start_value );
*
* The function crc_ccitt_generic() is a generic implementation of the CCITT
* algorithm for a one-pass calculation of the CRC for a byte string. The
* function accepts an initial start value for the crc.
*/

static uint16_t crc_ccitt_generic(const unsigned char* input_str, size_t num_bytes,
uint16_t start_value) {
uint16_t crc;
const unsigned char* ptr;
size_t a;

crc = start_value;
ptr = input_str;

if (ptr != NULL)
for (a = 0; a < num_bytes; a++) {
crc = (crc << 8) ^ crc_tabccitt[((crc >> 8) ^ (uint16_t)*ptr++) & 0x00FF];
}

return crc;

} /* crc_ccitt_generic */

/*
* uint16_t update_crc_ccitt( uint16_t crc, unsigned char c );
*
* The function update_crc_ccitt() calculates a new CRC-CCITT value based on
* the previous value of the CRC and the next byte of the data to be checked.
*/

uint16_t update_crc_ccitt(uint16_t crc, unsigned char c) {
return (crc << 8) ^ crc_tabccitt[((crc >> 8) ^ (uint16_t)c) & 0x00FF];

} /* update_crc_ccitt */
Loading

0 comments on commit ae9a3da

Please sign in to comment.