-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathcommon_voltronic-crc.c
290 lines (227 loc) · 7.46 KB
/
common_voltronic-crc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* common_voltronic-crc.c - Common CRC routines for Voltronic Power devices
*
* Copyright (C)
* 2014 Daniele Pezzini <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "common_voltronic-crc.h"
/* CRC table - filled at runtime by common_voltronic_crc_init() */
static unsigned short crc_table[256];
/* Flag (0/1) just to be sure all is properly initialized */
static int initialized = 0;
/* Fill CRC table: this function MUST be called before anything else that needs to perform CRC operations */
static void common_voltronic_crc_init(void)
{
unsigned short dividend;
/* Already been here */
if (initialized)
return;
/* Compute remainder of each possible dividend */
for (dividend = 0; dividend < 256; dividend++) {
unsigned short bit;
/* Start with dividend followed by zeros */
unsigned long remainder = dividend << 8;
/* Modulo 2 division, a bit at a time */
for (bit = 0; bit < 8; bit++)
/* Try to divide the current data bit */
if (remainder & 0x8000)
remainder = (remainder << 1) ^ 0x1021;
else
remainder <<= 1;
/* Store the result into the table */
crc_table[dividend] = remainder & 0xffff;
}
/* Ready, now */
initialized = 1;
}
/* See header file for details */
unsigned short common_voltronic_crc_compute(const char *input, const size_t len)
{
unsigned short byte, crc, crc_MSB, crc_LSB;
unsigned long remainder = 0;
/* Make sure all is ready */
if (!initialized)
common_voltronic_crc_init();
/* Divide *input* by the polynomial, a byte at a time */
for (byte = 0; byte < len; byte++)
remainder = (remainder << 8) ^ crc_table[(input[byte] ^ (remainder >> 8)) & 0xff];
/* The final remainder is the CRC */
crc = remainder & 0xffff;
/* Escape characters with a special meaning */
crc_MSB = (crc >> 8) & 0xff;
if (
crc_MSB == 10 || /* LF */
crc_MSB == 13 || /* CR */
crc_MSB == 40 /* ( */
)
crc_MSB++;
crc_LSB = crc & 0xff;
if (
crc_LSB == 10 || /* LF */
crc_LSB == 13 || /* CR */
crc_LSB == 40 /* ( */
)
crc_LSB++;
crc = ((crc_MSB & 0xff) << 8) + crc_LSB;
return crc;
}
/* See header file for details */
unsigned short common_voltronic_crc_calc(const char *input, const size_t inputlen)
{
size_t len;
char *cr = memchr(input, '\r', inputlen);
/* No CR, fall back to string length (and hope *input* doesn't contain inner '\0's) */
if (cr == NULL)
len = strlen(input);
else
len = cr - input;
/* At least 1 byte expected */
if (!len)
return -1;
/* Compute (and return) CRC */
return common_voltronic_crc_compute(input, len);
}
/* See header file for details */
int common_voltronic_crc_calc_and_add(const char *input, const size_t inputlen, char *output, const size_t outputlen)
{
unsigned short crc, crc_MSB, crc_LSB;
size_t len;
char *cr = memchr(input, '\r', inputlen);
/* No CR, fall back to string length (and hope *input* doesn't contain inner '\0's) */
if (cr == NULL)
len = strlen(input);
else
len = cr - input;
/* At least 1 byte expected */
if (!len)
return -1;
/* To accomodate CRC, *output* must have space for at least 2 bytes more than the actually used size of *input*.
* Also, pretend that *input* is a valid null-terminated string and so reserve the final byte in *output* for the terminating '\0'. */
if (
(cr == NULL && outputlen < len + 3) || /* 2-bytes CRC + 1 byte for terminating '\0' */
(cr != NULL && outputlen < len + 4) /* 2-bytes CRC + 1 byte for trailing CR + 1 byte for terminating '\0' */
)
return -1;
/* Compute CRC */
crc = common_voltronic_crc_compute(input, len);
crc_MSB = (crc >> 8) & 0xff;
crc_LSB = crc & 0xff;
/* Clear *output* */
memset(output, 0, outputlen);
/* Copy *input* to *output* */
memcpy(output, input, len);
/* Write CRC to *output* */
output[len++] = crc_MSB;
output[len++] = crc_LSB;
/* Reinstate the trailing CR in *output*, if appropriate */
if (cr != NULL)
output[len++] = '\r';
return (int)len;
}
/* See header file for details */
int common_voltronic_crc_calc_and_add_m(char *input, const size_t inputlen)
{
int len;
char buf[inputlen];
/* Compute CRC and copy *input*, with CRC added to it, to buf */
len = common_voltronic_crc_calc_and_add(input, inputlen, buf, inputlen);
/* Failed */
if (len == -1)
return -1;
/* Successfully computed CRC and copied *input*, with CRC added to it, to buf */
/* Clear *input* */
memset(input, 0, inputlen);
/* Copy back buf to *input* */
memcpy(input, buf, len);
return len;
}
/* See header file for details */
int common_voltronic_crc_check(const char *input, const size_t inputlen)
{
unsigned short crc, crc_MSB, crc_LSB;
char *cr = memchr(input, '\r', inputlen);
size_t len;
/* No CR, fall back to string length (and hope *input* doesn't contain inner '\0's) */
if (cr == NULL)
len = strlen(input);
else
len = cr - input;
/* Minimum length: 1 byte + 2 bytes CRC -> 3 */
if (len < 3)
return -1;
/* Compute CRC */
crc = common_voltronic_crc_compute(input, len - 2);
crc_MSB = (crc >> 8) & 0xff;
crc_LSB = crc & 0xff;
/* Fail */
if (
crc_MSB != (unsigned char)input[len - 2] ||
crc_LSB != (unsigned char)input[len - 1]
)
return -1;
/* Success */
return 0;
}
/* See header file for details */
int common_voltronic_crc_check_and_remove(const char *input, const size_t inputlen, char *output, const size_t outputlen)
{
char *cr;
size_t len;
/* Failed to check *input* CRC */
if (common_voltronic_crc_check(input, inputlen))
return -1;
/* *input* successfully validated -> remove CRC bytes */
cr = memchr(input, '\r', inputlen);
/* No CR, fall back to string length */
if (cr == NULL)
len = strlen(input);
else
len = cr - input;
/* *output* must have space for at least 2 bytes less than the actually used size of *input*.
* Also, pretend that *input* is a valid null-terminated string and so reserve the final byte in *output* for the terminating '\0'. */
len -= 2; /* Consider 2-bytes CRC length */
if (
(cr == NULL && outputlen < len + 1) || /* 1 byte for terminating '\0' */
(cr != NULL && outputlen < len + 2) /* 1 byte for terminating '\0' + 1 byte for trailing CR; 2-byte CRC */
)
return -1;
/* Clear *output* */
memset(output, 0, outputlen);
/* Copy *input* to *output* */
memcpy(output, input, len);
/* Reinstate the trailing CR in *output*, if appropriate */
if (cr != NULL)
output[len++] = '\r';
return (int)len;
}
/* See header file for details */
int common_voltronic_crc_check_and_remove_m(char *input, const size_t inputlen)
{
int len;
char buf[inputlen];
/* Check CRC and copy *input*, purged of the CRC, to buf */
len = common_voltronic_crc_check_and_remove(input, inputlen, buf, inputlen);
/* Failed */
if (len == -1)
return -1;
/* Successfully checked CRC and copied *input*, purged of the CRC, to buf */
/* Clear *input* */
memset(input, 0, inputlen);
/* Copy back buf to *input* */
memcpy(input, buf, len);
return len;
}