1
1
/*
2
- * Copyright (C) 2020 by Jonathan Naylor G4KLX
2
+ * Copyright (C) 2020,2021 by Jonathan Naylor G4KLX
3
3
*
4
4
* This program is free software; you can redistribute it and/or modify
5
5
* it under the terms of the GNU General Public License as published by
21
21
#include < cstdio>
22
22
#include < cassert>
23
23
24
- const uint16_t CRC_TABLE [] = {0x0000U , 0x5935U , 0xB26AU , 0xEB5FU , 0x3DE1U , 0x64D4U , 0x8F8BU , 0xD6BEU , 0x7BC2U , 0x22F7U , 0xC9A8U ,
24
+ const uint16_t CRC16_TABLE [] = {0x0000U , 0x5935U , 0xB26AU , 0xEB5FU , 0x3DE1U , 0x64D4U , 0x8F8BU , 0xD6BEU , 0x7BC2U , 0x22F7U , 0xC9A8U ,
25
25
0x909DU , 0x4623U , 0x1F16U , 0xF449U , 0xAD7CU , 0xF784U , 0xAEB1U , 0x45EEU , 0x1CDBU , 0xCA65U , 0x9350U ,
26
26
0x780FU , 0x213AU , 0x8C46U , 0xD573U , 0x3E2CU , 0x6719U , 0xB1A7U , 0xE892U , 0x03CDU , 0x5AF8U , 0xB63DU ,
27
27
0xEF08U , 0x0457U , 0x5D62U , 0x8BDCU , 0xD2E9U , 0x39B6U , 0x6083U , 0xCDFFU , 0x94CAU , 0x7F95U , 0x26A0U ,
@@ -46,12 +46,30 @@ const uint16_t CRC_TABLE[] = {0x0000U, 0x5935U, 0xB26AU, 0xEB5FU, 0x3DE1U, 0x64D
46
46
0xAC02U , 0xF537U , 0x2389U , 0x7ABCU , 0x91E3U , 0xC8D6U , 0x65AAU , 0x3C9FU , 0xD7C0U , 0x8EF5U , 0x584BU ,
47
47
0x017EU , 0xEA21U , 0xB314U };
48
48
49
- bool CM17CRC::checkCRC (const unsigned char * in, unsigned int nBytes)
49
+ const uint8_t CRC4_TABLE[] = {
50
+ 0x0 , 0x7 , 0xe , 0x9 , 0x5 , 0x2 , 0xb , 0xc , 0xa , 0xd , 0x4 , 0x3 , 0xf , 0x8 , 0x1 , 0x6 ,
51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, 0x1 , 0xd , 0xa , 0x3 , 0x4 ,
62
+ 0xa , 0xd , 0x4 , 0x3 , 0xf , 0x8 , 0x1 , 0x6 , 0x0 , 0x7 , 0xe , 0x9 , 0x5 , 0x2 , 0xb , 0xc ,
63
+ 0x7 , 0x0 , 0x9 , 0xe , 0x2 , 0x5 , 0xc , 0xb , 0xd , 0xa , 0x3 , 0x4 , 0x8 , 0xf , 0x6 , 0x1 ,
64
+ 0x9 , 0xe , 0x7 , 0x0 , 0xc , 0xb , 0x2 , 0x5 , 0x3 , 0x4 , 0xd , 0xa , 0x6 , 0x1 , 0x8 , 0xf ,
65
+ 0x4 , 0x3 , 0xa , 0xd , 0x1 , 0x6 , 0xf , 0x8 , 0xe , 0x9 , 0x0 , 0x7 , 0xb , 0xc , 0x5 , 0x2 };
66
+
67
+ bool CM17CRC::checkCRC16 (const unsigned char * in, unsigned int nBytes)
50
68
{
51
69
assert (in != NULL );
52
70
assert (nBytes > 2U );
53
71
54
- uint16_t crc = createCRC (in, nBytes - 2U );
72
+ uint16_t crc = createCRC16 (in, nBytes - 2U );
55
73
56
74
uint8_t temp[2U ];
57
75
temp[0U ] = (crc >> 8 ) & 0xFFU ;
@@ -60,26 +78,67 @@ bool CM17CRC::checkCRC(const unsigned char* in, unsigned int nBytes)
60
78
return temp[0U ] == in[nBytes - 2U ] && temp[1U ] == in[nBytes - 1U ];
61
79
}
62
80
63
- void CM17CRC::encodeCRC (unsigned char * in, unsigned int nBytes)
81
+ void CM17CRC::encodeCRC16 (unsigned char * in, unsigned int nBytes)
64
82
{
65
83
assert (in != NULL );
66
84
assert (nBytes > 2U );
67
85
68
- uint16_t crc = createCRC (in, nBytes - 2U );
86
+ uint16_t crc = createCRC16 (in, nBytes - 2U );
69
87
70
88
in[nBytes - 2U ] = (crc >> 8 ) & 0xFFU ;
71
89
in[nBytes - 1U ] = (crc >> 0 ) & 0xFFU ;
72
90
}
73
91
74
- uint16_t CM17CRC::createCRC (const unsigned char * in, unsigned int nBytes)
92
+ uint16_t CM17CRC::createCRC16 (const unsigned char * in, unsigned int nBytes)
75
93
{
76
94
assert (in != NULL );
77
95
78
96
uint16_t crc = 0xFFFFU ;
79
97
80
98
for (unsigned int i = 0U ; i < nBytes; i++)
81
- crc = (crc << 8 ) ^ CRC_TABLE [((crc >> 8 ) ^ uint16_t (in[i])) & 0x00FFU ];
99
+ crc = (crc << 8 ) ^ CRC16_TABLE [((crc >> 8 ) ^ uint16_t (in[i])) & 0x00FFU ];
82
100
83
101
return crc;
84
102
}
85
103
104
+ bool CM17CRC::checkCRC8 (unsigned char * in, unsigned int nBytes)
105
+ {
106
+ assert (in != NULL );
107
+ assert (nBytes > 1U );
108
+
109
+ uint8_t save = in[nBytes - 1U ] & 0x0FU ;
110
+
111
+ // Mask out the 4-bit CRC location
112
+ in[nBytes - 1U ] &= 0xF0U ;
113
+
114
+ uint8_t crc = createCRC8 (in, nBytes);
115
+
116
+ // Restore the original CRC
117
+ in[nBytes - 1U ] |= save;
118
+
119
+ return save == crc;
120
+ }
121
+
122
+ void CM17CRC::encodeCRC8 (unsigned char * in, unsigned int nBytes)
123
+ {
124
+ assert (in != NULL );
125
+ assert (nBytes > 1U );
126
+
127
+ in[nBytes - 1U ] &= 0xF0U ;
128
+
129
+ uint8_t crc = createCRC8 (in, nBytes);
130
+
131
+ in[nBytes - 1U ] |= crc & 0x0FU ;
132
+ }
133
+
134
+ uint8_t CM17CRC::createCRC8 (const unsigned char * in, unsigned int nBytes)
135
+ {
136
+ assert (in != NULL );
137
+
138
+ uint8_t crc = 0x0FU ;
139
+
140
+ for (unsigned int i = 0U ; i < nBytes; i++)
141
+ crc = CRC4_TABLE[crc ^ in[i]];
142
+
143
+ return crc;
144
+ }
0 commit comments