-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrc_vs_checksum.cpp
More file actions
278 lines (270 loc) · 7.1 KB
/
crc_vs_checksum.cpp
File metadata and controls
278 lines (270 loc) · 7.1 KB
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
#include "crc.h"
#include <algorithm>
#define crc12 "1100000001111"
#define rcrc12 "1111000000011"
#define DEBUG false
using namespace std;
bitset<13> compute(bitset<1000>temp, int length) {
bitset<13>c12(rcrc12), checksum;
//cout <<"length is " << length << endl;
//cout << "original c12: " <<c12.to_string() << endl;
//cout <<"original data: " << temp.to_string() << endl;
int pos = 0;
/*while (!temp[pos]) {
temp>>=1;
}*/
for (int i= 0; i < 13; i++) checksum[i] = temp[i];
bitset<1000>ans;
//cout <<"cs: "<< checksum.to_string() << endl;
//cout <<"begin at pos: " << pos << endl;
pos=12;
while (pos < length) {
/*
cout <<"==================================" << endl;
for (int i= 0; i < 13; i++) cout << checksum[i];
cout << endl << "pos: " << pos << endl;
cout <<"==================================" << endl;
*/
//cout <<"we got: " << checksum.to_string() << endl;
while (!checksum[0] && pos < length) {
ans[pos] = 0;
pos++;
checksum>>=1;
checksum[12] = temp[pos];
}
/*
cout <<"==================================" << endl;
for (int i= 0; i < 13; i++) cout << checksum[i];
cout << endl << "pos: " << pos << endl;
cout << endl <<"==================================" << endl;
if (pos+1 == length) {
cout <<"## pos is " << pos << endl;
break;
} else*/ if (pos < length && checksum[0]) {
checksum^=c12;
ans[pos] = 1;
}
}
#if DEBUG
cout <<"----------------answer-----------------" << endl << endl;
//checksum <<=1;
//cout <<"crc : " << checksum.to_string() << endl;
for (int i= 0; i < 12; i++) cout << checksum[i];
//cout <<"ans : " << ans.to_string() << endl;
cout <<endl <<"----------------------------------" << endl;
#endif
//cout <<"we got divide: " << ans.to_string() << endl;
//010111100100
return checksum;
}
bitset<8> internetChecksum (vector<bitset<9> > bt) {
//bool carry = false;
for (int i = 1; i < bt.size(); i++) {
#if DEBUG
cout <<"====== check bitset now ======" << endl;
for (int i = 0; i < bt.size(); i++)
for (int j = 0; j < 9; j++) cout << bt[i][j];
cout << endl;
cout << endl;
#endif
int carry = 0;
for (int j = 0; j < 9; j ++) {
if (carry) {
carry--;
if (!bt[0][j]) {
bt[0][j] = 1;
} else {
bt[0][j] = 0;
carry++;
}
}
if (bt[0][j]&bt[i][j]) {
bt[0][j] = 0;
carry++;
} else if (bt[0][j] | bt[i][j]) {
bt[0][j] = 1;
} else {
bt[0][j] = 0;
}
}
#if DEBUG
cout <<"####### result sum #########" << endl;
for (int j = 0; j < 9; j++) {
cout << bt[0][j];
}
cout << endl <<"###########################" << endl;
#endif
while (bt[0][8]) {
bt[0][8] = 0;
for (int j = 0; j < 9; j ++) {
if (bt[0][j]) {
bt[0][j] = 0;
} else {
bt[0][j] = 1;
break;
}
}
}
#if DEBUG
cout <<"####### result sum #########" << endl;
for (int j = 0; j < 9; j++) {
cout << bt[0][j];
}
cout << endl <<"###########################" << endl;
#endif
}
bitset<8>ans;
for (int i = 0; i < 8; i++) ans[i] = !bt[0][i];
return ans;
}
bool validChecksum(vector<bitset<9> > bt, bitset<9>checksum) {
for (int i = 1; i < bt.size(); i++) {
#if DEBUG
cout <<"====== check bitset now ======" << endl;
for (int i = 0; i < bt.size(); i++)
for (int j = 0; j < 9; j++) cout << bt[i][j];
cout << endl;
cout << endl;
#endif
int carry = 0;
for (int j = 0; j < 9; j ++) {
if (carry) {
carry--;
if (!bt[0][j]) {
bt[0][j] = 1;
} else {
bt[0][j] = 0;
carry++;
}
}
if (bt[0][j]&bt[i][j]) {
bt[0][j] = 0;
carry++;
} else if (bt[0][j] | bt[i][j]) {
bt[0][j] = 1;
} else {
bt[0][j] = 0;
}
}
while (bt[0][8]) {
bt[0][8] = 0;
for (int j = 0; j < 9; j ++) {
if (bt[0][j]) {
bt[0][j] = 0;
} else {
bt[0][j] = 1;
break;
}
}
}
}
#if DEBUG
cout <<"####### result sum #########" << endl;
for (int j = 0; j < 8; j++) cout << bt[0][j];
cout << endl;
for (int j = 0; j < 8; j++) cout << checksum[j];
cout << endl <<"###########################" << endl;
#endif
for (int i = 0; i < 8; i++) {
if (!((bt[0][i]&checksum[i]) == 0 && bt[0][i]|checksum[i])) return false;
}
return true;
}
int main () {
string line, flip;
int count = 0;
vector<bitset<1000> >data, wrong, flips;
vector<int>len;
ifstream infile("dataVs.txt");
//ifstream infile("bug.txt");
//start to read file;
while (getline(infile, line)) {
istringstream iss(line);
iss >> line >> flip;
//cout << line.size() <<" error: " << flip.size() << endl << endl;
reverse(line.begin(), line.end());
reverse(flip.begin(), flip.end());
bitset<1000>tmp(line);
bitset<1000>temp(line);
bitset<1000>tempp(flip);
len.push_back(line.size());
//cout << line.size() <<" , " << flip.size() << endl;
//cout << tmp.to_string() << endl;
for (int i = 0; i < len.back(); i++) {
if (tempp[i]) temp.flip(i);
//cout << temp[i];
}
//cout << endl;
data.push_back(tmp);
wrong.push_back(temp);
flips.push_back(tempp);
}
//lets compute crc checksum
cout <<"================ start =================" << endl;
for (int i = 0; i < data.size(); i++) {
bitset<13>crcOutcome = compute(data[i], len[i]+12);
string scrc, crcvalid;
scrc.pop_back();
for (int k = len[i], j = 0; j < 12 ;k++, j++) {
if (flips[i][k] == 0) wrong[i][k] = crcOutcome[j];
else wrong[i][k] = !crcOutcome[j];
if (crcOutcome[j]) scrc.push_back('1');
else scrc.push_back('0');
}
#if DEBUG
cout <<"---------------- the wrong bits --------------" << endl;
for (int j = 0, k = len[i]; j < 12; j++, k++) {
cout << wrong[i][k];
}
cout << endl <<"----------------------------------------------" << endl;
#endif
if (compute(wrong[i], len[i]+12).count() == 0) crcvalid = "pass";
else crcvalid = "not pass";
cout << "crc: "<< scrc <<" result: "<< crcvalid << endl;
//start to compute 2 parity checksum
count = len[i]/8;
vector<bitset<9> >group, ng;
for (int a = 0, j = 0; a < count; a++) {
bitset<9>tt, ff; //true and false data
for (int k = 7; k > -1; k--, j++) {
tt[k] = data[i][j];
ff[k] = wrong[i][j];
//cout << ff[k];
}
//cout << endl;
group.push_back(tt);
ng.push_back(ff);
}
#if DEBUG
cout <<"---------------- the wrong bits --------------" << endl;
for (int j = 0; j < ng.size(); j++) {
for (int k = 0; k < 8; k++) cout << ng[j][k];
cout << endl;
}
for (int j = 0; j < group.size(); j++) {
for (int k = 0; k < 8; k++) cout << group[j][k];
cout << endl;
}
cout << endl <<"----------------------------------------------" << endl;
#endif
bitset<8>cks = internetChecksum(group);
//make error checksum
bitset<9>fck;
for (int a = len[i], b = 7; b > -1; a++, b--) {
if (flips[i][a]) fck[b] = !cks[b];
else fck[b] = cks[b];
}
#if DEBUG
cout <<"---------------- the cks bits --------------" << endl;
for (int k = 0; k < 8; k++) cout << cks[k];
cout << endl;
for (int k = 0; k < 8; k++) cout << fck[k];
cout << endl;
cout << endl <<"----------------------------------------------" << endl;
#endif
if (validChecksum(ng, fck)) crcvalid = "pass";
else crcvalid = "not pass";
cout << "checksum: "<< cks.to_string() <<" result: "<< crcvalid << endl << endl;
}
return 0;
}