forked from oe5hpm/openBCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc.cpp
232 lines (181 loc) · 6.54 KB
/
crc.cpp
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
/***************************************************************
BayCom(R) Packet-Radio fuer IBM PC
OpenBCM-Mailbox
------------------------------
Berechnung von CRC-Pruefsummen
------------------------------
Copyright (C) Florian Radlherr
Taubenbergstr. 32
83627 Warngau
Alle Rechte vorbehalten / All Rights Reserved
***************************************************************/
//19990407 DH3MB CRCthp + CRCfbb: Changed unsigned long int to
// unsigned short int
//19990615 DH3MB Cleaned up the complete code, added CRC32 methods,
// made classes for every single CRC type,
// added #ifdef for all CRC types,
// moved file_crcthp to util.cpp
//19990626 DH3MB Added methods for updating the CRC using data blocks, rather
// than just passing one single byte every time
//19990703 DH3MB Added methods for updating the CRC with a string
#include "baycom.h"
/*---------------------------------------------------------------------------*/
// Include support for the various CRC types depending on what
// we actually need for the requested features
#define _CRC_THP // We need this for AutoBIN
#define _CRC_FBB // Only needed for FBB style forwarding
#ifdef FEATURE_BINSPLIT
#define _CRC_32 // Only needed when BIN split support is desired
#endif // FEATURE_BINSPLIT
#include "crctab.h" // This file must be included _here_
/*---------------------------------------------------------------------------*/
void crc::update (char *block, unsigned short int len)
//*************************************************************************
//
// CRC base class
//
//*************************************************************************
{
unsigned short int i = 0;
while (len--) update (block[i++]);
}
void crc::readfile (char *fname, off_t offset)
//*************************************************************************
//
// Reads a file and pushes the content into update()
//
//*************************************************************************
{
handle fd;
unsigned short int num;
char *buf;
if ((fd = s_open(fname, "srb")) == -1) return;
buf = (char *) t_malloc(1024, "crcf");
lseek(fd, offset, SEEK_SET);
while ((num = _read(fd, buf, 1024)) > 0)
{
update(buf, num);
waitfor(e_ticsfull);
}
t_free(buf);
s_close(fd);
}
/*---------------------------------------------------------------------------*/
#ifdef _CRC_THP
crcthp::crcthp (void)
//*************************************************************************
//
// Constructor: Just initialize the CRC
//
//*************************************************************************
{
result = (unsigned short int) 0x0000;
}
/*---------------------------------------------------------------------------*/
void crcthp::update (char ch)
//*************************************************************************
//
// Update the CRC-THP (used for AutoBin, 7plus line CRC)
//
//*************************************************************************
{
result = ccitt_crctab[result >> 8] ^ ((result << 8) | ch);
}
#endif // _CRC_THP
/*---------------------------------------------------------------------------*/
#ifdef _CRC_FBB
crcfbb::crcfbb (void)
//*************************************************************************
//
// Constructor: Just initialize the CRC
//
//*************************************************************************
{
result = (unsigned short int) 0x0000;
}
/*---------------------------------------------------------------------------*/
void crcfbb::update (char ch)
//*************************************************************************
//
// Update the CRC-FBB (used for FBB-type forwarding)
//
//*************************************************************************
{
result = ((result) << 8) ^ ccitt_crctab[((result) >> 8) ^ ch];
}
#endif // _CRC_FBB
/*---------------------------------------------------------------------------*/
#ifdef _CRC_FCS
crcfcs::crcfcs(void)
//*************************************************************************
//
// Constructor: Just initialize the CRC
//
//*************************************************************************
{
result = (unsigned short int) 0x0000;
}
/*---------------------------------------------------------------------------*/
void crcfcs::update (char ch)
//*************************************************************************
//
// Update the CRC-FCS
//
//*************************************************************************
{
result = (result >> 8) ^ ccitt_crctab[(result ^ ch) & 0xff];
}
#endif // _CRC_FCS
/*---------------------------------------------------------------------------*/
#ifdef _CRC_16
crc16::crc16 (void)
//*************************************************************************
//
// Constructor: Just initialize the CRC
//
//*************************************************************************
{
result = (unsigned short int) 0x0000;
}
/*---------------------------------------------------------------------------*/
void crc16::update (char ch)
//*************************************************************************
//
// Update the CRC-16
//
//*************************************************************************
{
result = (result >> 8) ^ crc16_table[(result ^ ch) & 0xff];
}
#endif // _CRC_16
/*---------------------------------------------------------------------------*/
#ifdef _CRC_32
//
// CRC32 functions derived from article Copyright (C) 1986 Stephen Satchell.
//
// Programmers may incorporate any or all code into their programs, giving
// proper credit within the source. Publication of the source routines is
// permitted so long as proper credit is given to Stephen Satchell, Satchell
// Evaluations and Chuck Forsberg, Omen Technology.
//
crc32::crc32 (void)
//*************************************************************************
//
// Constructor: Just initialize the CRC
//
//*************************************************************************
{
result = (unsigned long int) 0xffffffffL;
}
/*---------------------------------------------------------------------------*/
void crc32::update (char ch)
//*************************************************************************
//
// Update the CRC-32 (used for BIN splitting)
//
//*************************************************************************
{
result = crc32tab[((int)(result) ^ (char)(ch)) & 0xff] ^ ((result) >> 8);
}
#endif // _CRC_32
/*---------------------------------------------------------------------------*/