JaiaBot 1.10.0+14+g8dbf2589
JaiaBot micro-AUV software
 
Loading...
Searching...
No Matches
crc16.h
Go to the documentation of this file.
1#include <stdlib.h>
2
3#define CRC_POLY_16 0xA001
4#define CRC_START_16 0x0000
5#define CRC_TABLE_SIZE 64
6
7static void init_crc16_tab(void);
8
9static bool crc_tab16_init = false;
10static uint16_t crc_tab16[CRC_TABLE_SIZE];
11
12/*
13 * uint16_t crc_16( const unsigned char *input_str, size_t num_bytes );
14 *
15 * The function crc_16() calculates the 16 bits CRC16 in one pass for a byte
16 * string of which the beginning has been passed to the function. The number of
17 * bytes to check is also a parameter. The number of the bytes in the string is
18 * limited by the constant SIZE_MAX.
19 */
20
21uint16_t crc16(const void* input_str, size_t num_bytes)
22{
23 uint16_t crc;
24 const unsigned char* ptr;
25 size_t a;
26
27 if (!crc_tab16_init)
29
30 crc = CRC_START_16;
31 ptr = (const unsigned char*)input_str;
32
33 if (ptr != NULL)
34 for (a = 0; a < num_bytes; a++)
35 {
36 uint8_t index = (crc ^ (uint16_t) * (ptr++)) % CRC_TABLE_SIZE;
37 crc = (crc >> 8) ^ crc_tab16[index];
38 }
39
40 return crc;
41
42} /* crc_16 */
43
44/*
45 * uint16_t update_crc_16( uint16_t crc, unsigned char c );
46 *
47 * The function update_crc_16() calculates a new CRC-16 value based on the
48 * previous value of the CRC and the next byte of data to be checked.
49 */
50
51uint16_t update_crc_16(uint16_t crc, unsigned char c)
52{
53 if (!crc_tab16_init)
55
56 return (crc >> 8) ^ crc_tab16[(crc ^ (uint16_t)c) & 0x00FF];
57
58} /* update_crc_16 */
59
60/*
61 * static void init_crc16_tab( void );
62 *
63 * For optimal performance uses the CRC16 routine a lookup table with values
64 * that can be used directly in the XOR arithmetic in the algorithm. This
65 * lookup table is calculated by the init_crc16_tab() routine, the first time
66 * the CRC function is called.
67 */
68
69static void init_crc16_tab(void)
70{
71 uint16_t i;
72 uint16_t j;
73 uint16_t crc;
74 uint16_t c;
75
76 for (i = 0; i < CRC_TABLE_SIZE; i++)
77 {
78 crc = 0;
79 c = i;
80
81 for (j = 0; j < 8; j++)
82 {
83 if ((crc ^ c) & 0x0001)
84 crc = (crc >> 1) ^ CRC_POLY_16;
85 else
86 crc = crc >> 1;
87
88 c = c >> 1;
89 }
90
91 crc_tab16[i] = crc;
92 }
93
94 crc_tab16_init = true;
95
96} /* init_crc16_tab */
97
98uint16_t fletcher16(const void* input_str, size_t num_bytes)
99{
100 uint16_t sum = 0;
101 uint8_t* sum1 = (uint8_t*)&sum;
102 uint8_t* sum2 = sum1 + 1;
103
104 const uint8_t* buf = (uint8_t*)input_str;
105
106 for (int i = 0; i < num_bytes; i++)
107 {
108 (*sum1) += buf[i];
109 (*sum2) += (*sum1);
110 }
111
112 return sum;
113}
static uint16_t crc_tab16[CRC_TABLE_SIZE]
Definition crc16.h:10
#define CRC_POLY_16
Definition crc16.h:3
static void init_crc16_tab(void)
Definition crc16.h:69
uint16_t update_crc_16(uint16_t crc, unsigned char c)
Definition crc16.h:51
uint16_t crc16(const void *input_str, size_t num_bytes)
Definition crc16.h:21
static bool crc_tab16_init
Definition crc16.h:9
#define CRC_TABLE_SIZE
Definition crc16.h:5
#define CRC_START_16
Definition crc16.h:4
uint16_t fletcher16(const void *input_str, size_t num_bytes)
Definition crc16.h:98