-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Most commands implemented Very few cmds pending (low usage)
- Loading branch information
Showing
5 changed files
with
1,285 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/********************************************************************** | ||
* | ||
* Filename: crc.h | ||
* | ||
* Description: Slow and fast implementations of the CRC standards. | ||
* | ||
* | ||
* Copyright (c) 2000 by Michael Barr. This software is placed into | ||
* the public domain and may be used for any purpose. However, this | ||
* notice must not be changed or removed and no warranty is either | ||
* expressed or implied by its publication or distribution. | ||
**********************************************************************/ | ||
|
||
#ifndef CRC_H_INCLUDED | ||
#define CRC_H_INCLUDED | ||
|
||
|
||
#include <stdint.h> | ||
|
||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
|
||
typedef struct | ||
{ | ||
uint8_t bandwidth; | ||
uint32_t polynomial; | ||
uint32_t initial_value; | ||
uint32_t output_xor; | ||
uint8_t reflection_input; | ||
uint8_t reflection_output; | ||
} custom_crc_t; | ||
|
||
/* ---------- Defines for 16-bit CRC/XMODEM calculation (Not reflected) --------------------------------------------------------- */ | ||
#define CRC_16_RESULT_WIDTH 16u | ||
#define CRC_16_POLYNOMIAL 0x1021u | ||
#define CRC_16_INIT_VALUE 0x0000u | ||
#define CRC_16_XOR_VALUE 0x0000u | ||
|
||
// 16-bit CCITT | ||
//#define CRC_16_INIT_VALUE 0xFFFFu | ||
|
||
/* ---------- Defines for 32-bit CRC/CCITT calculation (Reflected) -------------------------------------------------------------- */ | ||
#define CRC_32_RESULT_WIDTH 32u | ||
#define CRC_32_POLYNOMIAL 0x04C11DB7u | ||
#define CRC_32_INIT_VALUE 0xFFFFFFFFu | ||
#define CRC_32_XOR_VALUE 0xFFFFFFFFu | ||
|
||
|
||
/** | ||
* This function makes a CRC16 calculation on Length data bytes | ||
* | ||
* RETURN VALUE: 16 bit result of CRC calculation | ||
*/ | ||
uint16_t crc16_hash(const uint8_t* message, int nBytes, uint16_t Init, uint16_t Poly, uint8_t RefIn, uint8_t RefOut); | ||
|
||
/** | ||
* This function makes a CRC32 calculation on Length data bytes | ||
* | ||
* RETURN VALUE: 32 bit result of CRC calculation | ||
*/ | ||
uint32_t crc32_hash(const uint8_t* message, int nBytes, uint32_t Init, uint32_t Poly, uint8_t RefIn, uint8_t RefOut); | ||
|
||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
|
||
#endif // CRC_H_INCLUDED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/********************************************************************** | ||
* | ||
* Filename: crc.c | ||
* | ||
* Description: Slow and fast implementations of the CRC standards. | ||
* | ||
* Notes: The parameters for each supported CRC standard are | ||
* defined in the header file crc.h. The implementations | ||
* here should stand up to further additions to that list. | ||
* | ||
* | ||
* Copyright (c) 2000 by Michael Barr. This software is placed into | ||
* the public domain and may be used for any purpose. However, this | ||
* notice must not be changed or removed and no warranty is either | ||
* expressed or implied by its publication or distribution. | ||
**********************************************************************/ | ||
|
||
#include <stdio.h> | ||
#include "crc_util.h" | ||
|
||
|
||
#define TOPBIT(W) (1 << (W - 1)) | ||
#define REFLECT_DATA(X) ((uint8_t) reflect((X), 8)) | ||
#define REFLECT_REMAINDER16(X) ((uint16_t) reflect((X), CRC_16_RESULT_WIDTH)) | ||
#define REFLECT_REMAINDER32(X) ((uint32_t) reflect((X), CRC_32_RESULT_WIDTH)) | ||
|
||
|
||
uint32_t reflect(uint32_t data, uint8_t nBits) | ||
{ | ||
uint32_t reflection = 0; | ||
uint8_t bit; | ||
/* | ||
* Reflect the data about the center bit. | ||
*/ | ||
for (bit = 0; bit < nBits; ++bit) | ||
{ | ||
/* | ||
* If the LSB bit is set, set the reflection of it. | ||
*/ | ||
if (data & 0x01) | ||
{ | ||
reflection |= (1 << ((nBits - 1) - bit)); | ||
} | ||
|
||
data = (data >> 1); | ||
} | ||
|
||
return (reflection); | ||
|
||
} /* reflect() */ | ||
|
||
uint16_t crc16_hash(const uint8_t* message, int nBytes, uint16_t Init, uint16_t Poly, uint8_t RefIn, uint8_t RefOut) | ||
{ | ||
uint16_t remainder = Init; | ||
int byte; | ||
uint8_t bit; | ||
|
||
/* | ||
* Perform modulo-2 division, a byte at a time. | ||
*/ | ||
for (byte = 0; byte < nBytes; ++byte) | ||
{ | ||
/* | ||
* Bring the next byte into the remainder. | ||
*/ | ||
remainder ^= ((RefIn ? REFLECT_DATA(message[byte]) : message[byte]) << (CRC_16_RESULT_WIDTH - 8)); | ||
|
||
/* | ||
* Perform modulo-2 division, a bit at a time. | ||
*/ | ||
for (bit = 8; bit > 0; --bit) | ||
{ | ||
/* | ||
* Try to divide the current data bit. | ||
*/ | ||
if (remainder & TOPBIT(CRC_16_RESULT_WIDTH)) | ||
{ | ||
remainder = (remainder << 1) ^ Poly; | ||
} | ||
else | ||
{ | ||
remainder = (remainder << 1); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* The final remainder is the CRC result. | ||
*/ | ||
return (RefOut ? REFLECT_REMAINDER16(remainder) : remainder); | ||
|
||
} /* crc16() */ | ||
|
||
|
||
uint32_t crc32_hash(const uint8_t* message, int nBytes, uint32_t Init, uint32_t Poly, uint8_t RefIn, uint8_t RefOut) | ||
{ | ||
uint32_t remainder = Init; | ||
int byte; | ||
uint8_t bit; | ||
|
||
/* | ||
* Perform modulo-2 division, a byte at a time. | ||
*/ | ||
for (byte = 0; byte < nBytes; ++byte) | ||
{ | ||
/* | ||
* Bring the next byte into the remainder. | ||
*/ | ||
remainder ^= ((RefIn ? REFLECT_DATA(message[byte]) : message[byte]) << (CRC_32_RESULT_WIDTH - 8)); | ||
|
||
/* | ||
* Perform modulo-2 division, a bit at a time. | ||
*/ | ||
for (bit = 8; bit > 0; --bit) | ||
{ | ||
/* | ||
* Try to divide the current data bit. | ||
*/ | ||
if (remainder & TOPBIT(CRC_32_RESULT_WIDTH)) | ||
{ | ||
remainder = (remainder << 1) ^ Poly; | ||
} | ||
else | ||
{ | ||
remainder = (remainder << 1); | ||
} | ||
} | ||
} | ||
|
||
/* | ||
* The final remainder is the CRC result. | ||
*/ | ||
return (RefOut ? REFLECT_REMAINDER32(remainder) : remainder); | ||
|
||
} /* crc32() */ |
Oops, something went wrong.