initial commit

This commit is contained in:
Syping 2020-10-25 02:56:19 +02:00
commit 93d4831bd4
6 changed files with 472 additions and 0 deletions

35
CMakeLists.txt Normal file
View File

@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.5)
project(crc16 LANGUAGES C)
project(crc32 LANGUAGES C)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(LIBCRC_HEADERS
libcrc-2.0/include/checksum.h
)
set(CRC16_SOURCES
src/plugin_crc16.c
libcrc-2.0/src/crc16.c
)
set(CRC32_SOURCES
src/plugin_crc32.c
libcrc-2.0/src/crc32.c
)
add_library(crc16 SHARED
${LIBCRC_HEADERS}
${CRC16_SOURCES}
)
add_library(crc32 SHARED
${LIBCRC_HEADERS}
${CRC32_SOURCES}
)
include_directories(libcrc-2.0/include)
install(TARGETS crc16 DESTINATION share/checkbrute/plugins)
install(TARGETS crc32 DESTINATION share/checkbrute/plugins)

View File

@ -0,0 +1,95 @@
/*
* Library: libcrc
* File: include/checksum.h
* 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 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
#include <stdint.h>
#include <stdio.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 0xEDB88320L
#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 0xFFFFFFFFL
/*
* 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 );
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 );
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 );
#endif // DEF_LIBCRC_CHECKSUM_H

169
libcrc-2.0/src/crc16.c Normal file
View File

@ -0,0 +1,169 @@
/*
* Library: libcrc
* File: src/crc16.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 source file src/crc16.c contains routines which calculate the common
* CRC16 cyclic redundancy check values for an incomming byte string.
*/
#include <stdbool.h>
#include <stdlib.h>
#include "checksum.h"
static void init_crc16_tab( void );
static bool crc_tab16_init = false;
static uint16_t crc_tab16[256];
/*
* uint16_t crc_16( const unsigned char *input_str, size_t num_bytes );
*
* The function crc_16() calculates the 16 bits CRC16 in one pass for a byte
* string of which the beginning has been passed to the function. The number of
* bytes to check is also a parameter. The number of the bytes in the string is
* limited by the constant SIZE_MAX.
*/
uint16_t crc_16( const unsigned char *input_str, size_t num_bytes ) {
uint16_t crc;
uint16_t tmp;
uint16_t short_c;
const unsigned char *ptr;
size_t a;
if ( ! crc_tab16_init ) init_crc16_tab();
crc = CRC_START_16;
ptr = input_str;
if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
short_c = 0x00ff & (uint16_t) *ptr;
tmp = crc ^ short_c;
crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];
ptr++;
}
return crc;
} /* crc_16 */
/*
* uint16_t crc_modbus( const unsigned char *input_str, size_t num_bytes );
*
* The function crc_modbus() calculates the 16 bits Modbus CRC in one pass for
* a byte string of which the beginning has been passed to the function. The
* number of bytes to check is also a parameter.
*/
uint16_t crc_modbus( const unsigned char *input_str, size_t num_bytes ) {
uint16_t crc;
uint16_t tmp;
uint16_t short_c;
const unsigned char *ptr;
size_t a;
if ( ! crc_tab16_init ) init_crc16_tab();
crc = CRC_START_MODBUS;
ptr = input_str;
if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
short_c = 0x00ff & (uint16_t) *ptr;
tmp = crc ^ short_c;
crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];
ptr++;
}
return crc;
} /* crc_modbus */
/*
* uint16_t update_crc_16( uint16_t crc, unsigned char c );
*
* The function update_crc_16() calculates a new CRC-16 value based on the
* previous value of the CRC and the next byte of data to be checked.
*/
uint16_t update_crc_16( uint16_t crc, unsigned char c ) {
uint16_t tmp;
uint16_t short_c;
short_c = 0x00ff & (uint16_t) c;
if ( ! crc_tab16_init ) init_crc16_tab();
tmp = crc ^ short_c;
crc = (crc >> 8) ^ crc_tab16[ tmp & 0xff ];
return crc;
} /* update_crc_16 */
/*
* static void init_crc16_tab( void );
*
* For optimal performance uses the CRC16 routine a lookup table with values
* that can be used directly in the XOR arithmetic in the algorithm. This
* lookup table is calculated by the init_crc16_tab() routine, the first time
* the CRC function is called.
*/
static void init_crc16_tab( void ) {
uint16_t i;
uint16_t j;
uint16_t crc;
uint16_t c;
for (i=0; i<256; i++) {
crc = 0;
c = i;
for (j=0; j<8; j++) {
if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ CRC_POLY_16;
else crc = crc >> 1;
c = c >> 1;
}
crc_tab16[i] = crc;
}
crc_tab16_init = true;
} /* init_crc16_tab */

131
libcrc-2.0/src/crc32.c Normal file
View File

@ -0,0 +1,131 @@
/*
* Library: libcrc
* File: src/crc32.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 source file src/crc32.c contains the routines which are needed to
* calculate a 32 bit CRC value of a sequence of bytes.
*/
#include <stdbool.h>
#include <stdlib.h>
#include "checksum.h"
static void init_crc32_tab( void );
static bool crc_tab32_init = false;
static uint32_t crc_tab32[256];
/*
* uint32_t crc_32( const unsigned char *input_str, size_t num_bytes );
*
* The function crc_32() calculates in one pass the common 32 bit CRC value for
* a byte string that is passed to the function together with a parameter
* indicating the length.
*/
uint32_t crc_32( const unsigned char *input_str, size_t num_bytes ) {
uint32_t crc;
uint32_t tmp;
uint32_t long_c;
const unsigned char *ptr;
size_t a;
if ( ! crc_tab32_init ) init_crc32_tab();
crc = CRC_START_32;
ptr = input_str;
if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
long_c = 0x000000FFL & (uint32_t) *ptr;
tmp = crc ^ long_c;
crc = (crc >> 8) ^ crc_tab32[ tmp & 0xff ];
ptr++;
}
crc ^= 0xffffffffL;
return crc & 0xffffffffL;
} /* crc_32 */
/*
* uint32_t update_crc_32( uint32_t crc, unsigned char c );
*
* The function update_crc_32() calculates a new CRC-32 value based on the
* previous value of the CRC and the next byte of the data to be checked.
*/
uint32_t update_crc_32( uint32_t crc, unsigned char c ) {
uint32_t tmp;
uint32_t long_c;
long_c = 0x000000ffL & (uint32_t) c;
if ( ! crc_tab32_init ) init_crc32_tab();
tmp = crc ^ long_c;
crc = (crc >> 8) ^ crc_tab32[ tmp & 0xff ];
return crc & 0xffffffffL;;
} /* update_crc_32 */
/*
* static void init_crc32_tab( void );
*
* For optimal speed, the CRC32 calculation uses a table with pre-calculated
* bit patterns which are used in the XOR operations in the program. This table
* is generated once, the first time the CRC update routine is called.
*/
static void init_crc32_tab( void ) {
uint32_t i;
uint32_t j;
uint32_t crc;
for (i=0; i<256; i++) {
crc = i;
for (j=0; j<8; j++) {
if ( crc & 0x00000001L ) crc = ( crc >> 1 ) ^ CRC_POLY_32;
else crc = crc >> 1;
}
crc_tab32[i] = crc;
}
crc_tab32_init = true;
} /* init_crc32_tab */

21
src/plugin_crc16.c Normal file
View File

@ -0,0 +1,21 @@
#include "checksum.h"
const char* checkbrute_format()
{
return "CRC16";
}
const char* checkbrute_version()
{
return "0.1";
}
uint64_t checkbrute_hash64(unsigned char* data, size_t size)
{
return (uint64_t)crc_16(data, size);
}
int checkbrute_hashsz()
{
return 2;
}

21
src/plugin_crc32.c Normal file
View File

@ -0,0 +1,21 @@
#include "checksum.h"
const char* checkbrute_format()
{
return "CRC32";
}
const char* checkbrute_version()
{
return "0.1";
}
uint64_t checkbrute_hash64(unsigned char* data, size_t size)
{
return (uint64_t)crc_32(data, size);
}
int checkbrute_hashsz()
{
return 4;
}