-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
216 lines (188 loc) · 7.75 KB
/
main.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
#include <iostream>
#include <iomanip>
#include <algorithm>
#include "CrcParameters.h"
#include "Crc8.h"
#include "Crc16.h"
#include "Crc32.h"
#include "Crc64.h"
CrcTypes crcTypes;
Crc8 crc8;
Crc16 crc16;
Crc32 crc32;
Crc64 crc64;
//*****************************************************************************
void ListTypes()
{
std::cout << "Valid CRC types:\n";
// List CRC-8 types
for (CrcTypes::const_iterator itr = crcTypes.begin(); itr != crcTypes.end(); ++itr)
{
if (itr->first.substr(0, 4) == "crc8")
{
const CrcParameters& parameters = itr->second;
std::cout << std::left << std::setfill(' ') << std::setw(20) << itr->first
<< " : "
<< "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << int(std::get<uint8_t>(parameters.polynomial)) << " : "
<< std::boolalpha << std::left << std::setw(5) << std::setfill(' ')
<< parameters.reflectIn << " : "
<< parameters.reflectOut
<< "\n";
}
}
std::cout << "\n";
// List CRC-16 types
for (CrcTypes::const_iterator itr = crcTypes.begin(); itr != crcTypes.end(); ++itr)
{
if (itr->first.substr(0, 5) == "crc16")
{
const CrcParameters& parameters = itr->second;
std::cout << std::left << std::setfill(' ') << std::setw(20) << itr->first
<< " : "
<< "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(4) << std::get<uint16_t>(parameters.polynomial) << " : "
<< std::boolalpha << std::left << std::setw(5) << std::setfill(' ')
<< parameters.reflectIn << " : "
<< parameters.reflectOut
<< "\n";
}
}
std::cout << "\n";
// List CRC-32 types
for (CrcTypes::const_iterator itr = crcTypes.begin(); itr != crcTypes.end(); ++itr)
{
if (itr->first.substr(0, 5) == "crc32")
{
const CrcParameters& parameters = itr->second;
std::cout << std::left << std::setfill(' ') << std::setw(20) << itr->first
<< " : "
<< "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(8) << std::get<uint32_t>(parameters.polynomial) << " : "
<< std::boolalpha << std::left << std::setw(5) << std::setfill(' ')
<< parameters.reflectIn << " : "
<< parameters.reflectOut
<< "\n";
}
}
std::cout << "\n";
// List CRC-64 types
for (CrcTypes::const_iterator itr = crcTypes.begin(); itr != crcTypes.end(); ++itr)
{
if (itr->first.substr(0, 5) == "crc64")
{
const CrcParameters& parameters = itr->second;
std::cout << std::left << std::setfill(' ') << std::setw(20) << itr->first
<< " : "
<< "0x" << std::uppercase << std::hex << std::setfill('0') << std::setw(16) << std::get<uint64_t>(parameters.polynomial) << " : "
<< std::boolalpha << std::left << std::setw(5) << std::setfill(' ')
<< parameters.reflectIn << " : "
<< parameters.reflectOut
<< "\n";
}
}
std::cout << "\n";
}
//*****************************************************************************
void Initialise()
{
crcTypes["crc8-ccitt"] = { uint8_t(0x07), false, false };
crcTypes["crc8-rohc"] = { uint8_t(0x07), true, true };
crcTypes["crc8-cdma2000"] = { uint8_t(0x9B), false, false };
crcTypes["crc8-wcdma"] = { uint8_t(0x9B), true, true };
crcTypes["crc8-ebu"] = { uint8_t(0x1D), true, true };
crcTypes["crc8-i-code"] = { uint8_t(0x1D), false, false };
crcTypes["crc8-darc"] = { uint8_t(0x39), true, true };
crcTypes["crc8-dvb-s2"] = { uint8_t(0xD5), false, false };
crcTypes["crc8-itu"] = { uint8_t(0x07), false, false };
crcTypes["crc8-maxim"] = { uint8_t(0x31), true, true };
crcTypes["crc16-ccitt"] = { uint16_t(0x1021), false, false };
crcTypes["crc16-aug-ccitt"] = { uint16_t(0x1021), false, false };
crcTypes["crc16-genibus"] = { uint16_t(0x1021), false, false };
crcTypes["crc16-xmodem"] = { uint16_t(0x1021), false, false };
crcTypes["crc16-mcrf4xx"] = { uint16_t(0x1021), true, true };
crcTypes["crc16-riello"] = { uint16_t(0x1021), true, true };
crcTypes["crc16-tms37157"] = { uint16_t(0x1021), true, true };
crcTypes["crc16-a"] = { uint16_t(0x1021), true, true };
crcTypes["crc16-kermit"] = { uint16_t(0x1021), true, true };
crcTypes["crc16-x25"] = { uint16_t(0x1021), true, true };
crcTypes["crc16-buypass"] = { uint16_t(0x8005), false, false };
crcTypes["crc16-dds-110"] = { uint16_t(0x8005), false, false };
crcTypes["crc16-arc"] = { uint16_t(0x8005), true, true };
crcTypes["crc16-maxim"] = { uint16_t(0x8005), true, true };
crcTypes["crc16-usb"] = { uint16_t(0x8005), true, true };
crcTypes["crc16-modbus"] = { uint16_t(0x8005), true, true };
crcTypes["crc16-dect-r"] = { uint16_t(0x0589), false, false };
crcTypes["crc16-dect-x"] = { uint16_t(0x0589), false, false };
crcTypes["crc16-en-13757"] = { uint16_t(0x3D65), false, false };
crcTypes["crc16-dnp"] = { uint16_t(0x3D65), true, true };
crcTypes["crc16-cdma2000"] = { uint16_t(0xC867), false, false };
crcTypes["crc16-teledisk"] = { uint16_t(0xA097), false, false };
crcTypes["crc32-bzip2"] = { uint32_t(0x04C11DB7), false, false };
crcTypes["crc32-mpeg2"] = { uint32_t(0x04C11DB7), false, false };
crcTypes["crc32-posix"] = { uint32_t(0x04C11DB7), false, false };
crcTypes["crc32"] = { uint32_t(0x04C11DB7), true, true };
crcTypes["crc32-jamcrc"] = { uint32_t(0x04C11DB7), true, true };
crcTypes["crc32-q"] = { uint32_t(0x814141AB), false, false };
crcTypes["crc32-xfr"] = { uint32_t(0x000000AF), false, false };
crcTypes["crc32-c"] = { uint32_t(0x1EDC6F41), false, false };
crcTypes["crc32-d"] = { uint32_t(0xA833982B), false, false };
crcTypes["crc64-ecma"] = { uint64_t(0x42F0E1EBA9EA3693), false, false };
}
//*****************************************************************************
int main(int argc, char* argv[])
{
Initialise();
if (argc > 1)
{
std::string crcTypeName(argv[1]);
CrcTypes::const_iterator itr = crcTypes.find(crcTypeName);
if (itr != crcTypes.end())
{
std::cout << "Generating CRC table for " << crcTypeName << "\n\n";
const CrcParameters& parameters = itr->second;
// What size of CRC is it?
CrcParameters::Type crcType = CrcParameters::Type(parameters.polynomial.index());
switch (crcType)
{
case CrcParameters::Type::CRC8:
{
crc8.GenerateTable(std::get<uint8_t>(parameters.polynomial), parameters.reflectIn, parameters.reflectOut);
crc8.DisplayTable();
break;
}
case CrcParameters::Type::CRC16:
{
crc16.GenerateTable(std::get<uint16_t>(parameters.polynomial), parameters.reflectIn, parameters.reflectOut);
crc16.DisplayTable();
break;
}
case CrcParameters::Type::CRC32:
{
crc32.GenerateTable(std::get<uint32_t>(parameters.polynomial), parameters.reflectIn, parameters.reflectOut);
crc32.DisplayTable();
break;
}
case CrcParameters::Type::CRC64:
{
crc64.GenerateTable(std::get<uint64_t>(parameters.polynomial), parameters.reflectIn, parameters.reflectOut);
crc64.DisplayTable();
break;
}
default:
{
std::cout << "Unsupported CRC size\n\n";
break;
}
}
}
else
{
std::cout << "Unknown CRC type : " << crcTypeName << "\n";
ListTypes();
}
}
else
{
std::cout << "\nSyntax: crc-table <crc name>\n";
std::cout << "Example: crc-table crc8-ccitt\n\n";
ListTypes();
}
}