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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
| //------------------------------------------------------------------------------
#include "cs.h"
//==============================================================================
//lrc = -( (c[0] + c[1] + ... + c[n]) % 256 )
unsigned char lrc_modicon
(
unsigned char *puchMsg, /* message to calculate LRC upon */
unsigned long usDataLen /* quantity of bytes in message */
)
{
unsigned char uchLRC = 0; /* LRC char initialized */
for(unsigned long i = 0; i < usDataLen; i++)
uchLRC += *puchMsg++; /* add buffer byte without carry */
return -uchLRC; /* return twos complement */
}
//------------------------------------------------------------------------------
//lrc = (c[0] + c[1] + ... + c[n]) % 256
unsigned char lrc_icp_das
(
unsigned char *message,
unsigned long sizeof_message
)
{
unsigned char res = 0;
for(unsigned long i = 0; i < sizeof_message; i++)
res += *message++;
return res;
}
//------------------------------------------------------------------------------
unsigned char lrc_volmag
(
unsigned char *message,
unsigned long sizeof_message
)
{
int res = 0;
for(unsigned long i = 0; i < sizeof_message; i++)
{
res += *message++;
if (res > 255) res -= 255;
}
return -res;
}
//==============================================================================
/* Table of CRC values for low–order byte */
static const unsigned char auchCRCLo[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01,
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40
};
//------------------------------------------------------------------------------
/* Table of CRC values for high–order byte */
static const char auchCRCHi[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7,
0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE,
0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2,
0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB,
0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91,
0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88,
0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80,
0x40
};
//------------------------------------------------------------------------------
//IBM, 0x8005, x16 + x15 + x2 + 1
unsigned short crc16_ibm
(
unsigned char *puchMsg, /* message to calculate CRC upon */
unsigned long usDataLen /* quantity of bytes in message */
)
{
unsigned char uchCRCLo = 0xFF; /* low byte of CRC initialized */
unsigned char uchCRCHi = 0xFF; /* high byte of CRC initialized */
unsigned long uIndex; /* will index into CRC lookup table */
for(unsigned long i = 0; i < usDataLen; i++)
{
uIndex = uchCRCLo ^ *puchMsg++; /* calculate the CRC */
uchCRCLo = uchCRCHi ^ auchCRCLo[uIndex];
uchCRCHi = auchCRCHi[uIndex];
}
return (uchCRCHi << 8 | uchCRCLo);
}
//==============================================================================
static unsigned short crc16_ccitt_c[256] =
{
0x0000, 0x2110, 0x4220, 0x6330, 0x8440, 0xa550, 0xc660, 0xe770,
0x0881, 0x2991, 0x4aa1, 0x6bb1, 0x8cc1, 0xadd1, 0xcee1, 0xeff1,
0x3112, 0x1002, 0x7332, 0x5222, 0xb552, 0x9442, 0xf772, 0xd662,
0x3993, 0x1883, 0x7bb3, 0x5aa3, 0xbdd3, 0x9cc3, 0xfff3, 0xdee3,
0x6224, 0x4334, 0x2004, 0x0114, 0xe664, 0xc774, 0xa444, 0x8554,
0x6aa5, 0x4bb5, 0x2885, 0x0995, 0xeee5, 0xcff5, 0xacc5, 0x8dd5,
0x5336, 0x7226, 0x1116, 0x3006, 0xd776, 0xf666, 0x9556, 0xb446,
0x5bb7, 0x7aa7, 0x1997, 0x3887, 0xdff7, 0xfee7, 0x9dd7, 0xbcc7,
0xc448, 0xe558, 0x8668, 0xa778, 0x4008, 0x6118, 0x0228, 0x2338,
0xccc9, 0xedd9, 0x8ee9, 0xaff9, 0x4889, 0x6999, 0x0aa9, 0x2bb9,
0xf55a, 0xd44a, 0xb77a, 0x966a, 0x711a, 0x500a, 0x333a, 0x122a,
0xfddb, 0xdccb, 0xbffb, 0x9eeb, 0x799b, 0x588b, 0x3bbb, 0x1aab,
0xa66c, 0x877c, 0xe44c, 0xc55c, 0x222c, 0x033c, 0x600c, 0x411c,
0xaeed, 0x8ffd, 0xeccd, 0xcddd, 0x2aad, 0x0bbd, 0x688d, 0x499d,
0x977e, 0xb66e, 0xd55e, 0xf44e, 0x133e, 0x322e, 0x511e, 0x700e,
0x9fff, 0xbeef, 0xdddf, 0xfccf, 0x1bbf, 0x3aaf, 0x599f, 0x788f,
0x8891, 0xa981, 0xcab1, 0xeba1, 0x0cd1, 0x2dc1, 0x4ef1, 0x6fe1,
0x8010, 0xa100, 0xc230, 0xe320, 0x0450, 0x2540, 0x4670, 0x6760,
0xb983, 0x9893, 0xfba3, 0xdab3, 0x3dc3, 0x1cd3, 0x7fe3, 0x5ef3,
0xb102, 0x9012, 0xf322, 0xd232, 0x3542, 0x1452, 0x7762, 0x5672,
0xeab5, 0xcba5, 0xa895, 0x8985, 0x6ef5, 0x4fe5, 0x2cd5, 0x0dc5,
0xe234, 0xc324, 0xa014, 0x8104, 0x6674, 0x4764, 0x2454, 0x0544,
0xdba7, 0xfab7, 0x9987, 0xb897, 0x5fe7, 0x7ef7, 0x1dc7, 0x3cd7,
0xd326, 0xf236, 0x9106, 0xb016, 0x5766, 0x7676, 0x1546, 0x3456,
0x4cd9, 0x6dc9, 0x0ef9, 0x2fe9, 0xc899, 0xe989, 0x8ab9, 0xaba9,
0x4458, 0x6548, 0x0678, 0x2768, 0xc018, 0xe108, 0x8238, 0xa328,
0x7dcb, 0x5cdb, 0x3feb, 0x1efb, 0xf98b, 0xd89b, 0xbbab, 0x9abb,
0x754a, 0x545a, 0x376a, 0x167a, 0xf10a, 0xd01a, 0xb32a, 0x923a,
0x2efd, 0x0fed, 0x6cdd, 0x4dcd, 0xaabd, 0x8bad, 0xe89d, 0xc98d,
0x267c, 0x076c, 0x645c, 0x454c, 0xa23c, 0x832c, 0xe01c, 0xc10c,
0x1fef, 0x3eff, 0x5dcf, 0x7cdf, 0x9baf, 0xbabf, 0xd98f, 0xf89f,
0x176e, 0x367e, 0x554e, 0x745e, 0x932e, 0xb23e, 0xd10e, 0xf01e
};
//------------------------------------------------------------------------------
//CCITT, 0x1021, x16 + x12 + x5 + 1
unsigned short crc16_ccitt
(
unsigned char *message,
unsigned long sizeof_message
)
{
unsigned short res = 0;
for(unsigned long i = 0; i < sizeof_message; i++)
{
res ^= message[i];
res = (res >> 8) ^ crc16_ccitt_c[res & 0xFF];
}
return res;
}
//==============================================================================
void set_lrc_modicon(unsigned char * const res)
{
*res = 0;
}
//------------------------------------------------------------------------------
void update_lrc_modicon(unsigned char * const res, const unsigned char c)
{
*res += c;
}
//------------------------------------------------------------------------------
void get_lrc_modicon(unsigned char * const res)
{
*res = -*res;
}
//==============================================================================
void set_lrc_icp_das(unsigned char * const res)
{
*res = 0;
}
//------------------------------------------------------------------------------
void update_lrc_icp_das(unsigned char * const res, const unsigned char c)
{
*res += c;
}
//------------------------------------------------------------------------------
void get_lrc_icp_das(unsigned char * const res)
{}
//==============================================================================
void set_lrc_volmag(unsigned char * const res)
{
*res = 0;
}
//------------------------------------------------------------------------------
void update_lrc_volmag(unsigned char * const res, const unsigned char c)
{
int s = 0;
*(unsigned char *)&s = *res;
s += c;
if (s > 255) s -= 255;
*res = *(unsigned char *)&s;
}
//------------------------------------------------------------------------------
void get_lrc_volmag(unsigned char * const res)
{
int s = 0;
*(unsigned char *)&s = *res;
*res = -s;
}
//==============================================================================
void set_crc16_ibm(unsigned short * const res)
{
*res = 0xFFFF;
}
//------------------------------------------------------------------------------
void update_crc16_ibm(unsigned short * const res, const unsigned char c)
{
unsigned char *res_lo = (unsigned char *)res;
unsigned char *res_hi = (unsigned char *)res + 1;
unsigned long index = *res_lo ^ c;
*res_lo = *res_hi ^ auchCRCLo[index];
*res_hi = auchCRCHi[index];
}
//------------------------------------------------------------------------------
void get_crc16_ibm(unsigned short * const res)
{}
//==============================================================================
void set_crc16_ccitt(unsigned short * const res)
{
*res = 0;
}
//------------------------------------------------------------------------------
void update_crc16_ccitt(unsigned short * const res, const unsigned char c)
{
*res ^= c;
*res = (*res >> 8) ^ crc16_ccitt_c[*res & 0xFF];
}
//------------------------------------------------------------------------------
void get_crc16_ccitt(unsigned short * const res)
{}
//------------------------------------------------------------------------------
unsigned char lrc_modicon(std::istream &is)
{
unsigned char res;
set_lrc_modicon(&res);
while (true)
{
int c = is.peek();
if (c == EOF) break;
is.ignore();
update_lrc_modicon(&res, c);
}
get_lrc_modicon(&res);
return res;
}
//------------------------------------------------------------------------------
unsigned char lrc_icp_das(std::istream &is)
{
unsigned char res;
set_lrc_icp_das(&res);
while (true)
{
int c = is.peek();
if (c == EOF) break;
is.ignore();
update_lrc_icp_das(&res, c);
}
get_lrc_icp_das(&res);
return res;
}
//------------------------------------------------------------------------------
unsigned char lrc_volmag(std::istream &is)
{
unsigned char res;
set_lrc_volmag(&res);
while (true)
{
int c = is.peek();
if (c == EOF) break;
is.ignore();
update_lrc_volmag(&res, c);
}
get_lrc_volmag(&res);
return res;
}
//------------------------------------------------------------------------------
unsigned short crc16_ibm(std::istream &is)
{
unsigned short res;
set_crc16_ibm(&res);
while (true)
{
int c = is.peek();
if (c == EOF) break;
is.ignore();
update_crc16_ibm(&res, c);
}
get_crc16_ibm(&res);
return res;
}
//------------------------------------------------------------------------------
unsigned short crc16_ccitt(std::istream &is)
{
unsigned short res;
set_crc16_ccitt(&res);
while (true)
{
int c = is.peek();
if (c == EOF) break;
is.ignore();
update_crc16_ccitt(&res, c);
}
get_crc16_ccitt(&res);
return res;
} |