Replace the CRC16-CCITT implementation with one generated by pycrc to be MIT compliant.

This commit is contained in:
Admiral H. Curtiss
2016-10-30 02:49:27 +02:00
parent db6d6b64b6
commit f4aee57e89
6 changed files with 193 additions and 62 deletions

53
Checksums/ccitt.c Normal file
View File

@@ -0,0 +1,53 @@
/**
* \file ccitt.c
* Functions and types for CRC checks.
*
* Generated on Sun Oct 30 02:18:53 2016,
* by pycrc v0.9, https://pycrc.org
* using the configuration:
* Width = 16
* Poly = 0x1021
* Xor_In = 0xffff
* ReflectIn = False
* Xor_Out = 0x0000
* ReflectOut = False
* Algorithm = bit-by-bit-fast
*****************************************************************************/
#include "ccitt.h" /* include the header file generated with pycrc */
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
/**
* Update the crc value with new data.
*
* \param crc The current crc value.
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
crc_t crc_update(crc_t crc, const void *data, size_t data_len)
{
const unsigned char *d = (const unsigned char *)data;
unsigned int i;
bool bit;
unsigned char c;
while (data_len--) {
c = *d++;
for (i = 0x80; i > 0; i >>= 1) {
bit = crc & 0x8000;
if (c & i) {
bit = !bit;
}
crc <<= 1;
if (bit) {
crc ^= 0x1021;
}
}
crc &= 0xffff;
}
return crc & 0xffff;
}

55
Checksums/ccitt.cs Normal file
View File

@@ -0,0 +1,55 @@
// C# port of the ccitt.c/ccitt.h files in this directory.
// Original files generated by pycrc v0.9, https://pycrc.org
namespace Checksums {
public static class Ccitt {
/**
* Calculate the initial crc value.
*
* \return The initial crc value.
*****************************************************************************/
public static ushort Init() {
return 0xffff;
}
/**
* Update the crc value with new data.
*
* \param crc The current crc value.
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
public static ushort Update( ushort crc, byte[] data, long data_len ) {
uint i;
bool bit;
byte c;
for ( long idx = 0; idx < data_len; ++idx ) {
c = data[idx];
for ( i = 0x80; i > 0; i >>= 1 ) {
bit = ( crc & 0x8000 ) != 0;
if ( ( c & i ) != 0 ) {
bit = !bit;
}
crc <<= 1;
if ( bit ) {
crc ^= 0x1021;
}
}
crc &= 0xffff;
}
return (ushort)( crc & 0xffff );
}
/**
* Calculate the final crc value.
*
* \param crc The current crc value.
* \return The final crc value.
*****************************************************************************/
public static ushort Finalize( ushort crc ) {
return (ushort)( crc ^ 0x0000 );
}
}
}

82
Checksums/ccitt.h Normal file
View File

@@ -0,0 +1,82 @@
/**
* \file ccitt.h
* Functions and types for CRC checks.
*
* Generated on Sun Oct 30 02:18:48 2016,
* by pycrc v0.9, https://pycrc.org
* using the configuration:
* Width = 16
* Poly = 0x1021
* Xor_In = 0xffff
* ReflectIn = False
* Xor_Out = 0x0000
* ReflectOut = False
* Algorithm = bit-by-bit-fast
*****************************************************************************/
#ifndef __CCITT_H__
#define __CCITT_H__
#include <stdlib.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* The definition of the used algorithm.
*
* This is not used anywhere in the generated code, but it may be used by the
* application code to call algoritm-specific code, is desired.
*****************************************************************************/
#define CRC_ALGO_BIT_BY_BIT_FAST 1
/**
* The type of the CRC values.
*
* This type must be big enough to contain at least 16 bits.
*****************************************************************************/
typedef uint_fast16_t crc_t;
/**
* Calculate the initial crc value.
*
* \return The initial crc value.
*****************************************************************************/
static inline crc_t crc_init(void)
{
return 0xffff;
}
/**
* Update the crc value with new data.
*
* \param crc The current crc value.
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
crc_t crc_update(crc_t crc, const void *data, size_t data_len);
/**
* Calculate the final crc value.
*
* \param crc The current crc value.
* \return The final crc value.
*****************************************************************************/
static inline crc_t crc_finalize(crc_t crc)
{
return crc ^ 0x0000;
}
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif
#endif /* __CCITT_H__ */