eccSamsung.c
7.12 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
/*******************************************************************/
/* Revision History */
/* Revision 2.1 was revised for speed-up */
/* Revision 3.0 was revised for speed-up */
/* Description : The following source code shows an example for */
/* generating of 3bytes per 256bytes, based on hamming code ecc */
/*******************************************************************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
typedef unsigned int uint;
typedef unsigned char uchar;
/*
Reading DATA file
*/
get_data_gen(unsigned char *file_name,unsigned char *data)
{
int i,j;
FILE *fdata;
if ((fdata=fopen(file_name,"r"))==0) { /* Open DATA file */
printf("Can't open '%s'!\7\n",file_name);
exit(0);
}
for(i=0; i<256; ++i){
fscanf(fdata,"%x",&j); /* Reading DATA */
data[i]=(unsigned char)j;
}
fclose(fdata); /* Close DATA file */
}
main(int argc,char *argv[])
{
uint i, j;
uchar ecc_gen[3],write_data[256],paritr[256], tmp = 0, tmp2=0;
uchar data_table0[16] = {0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0};
uchar data_table1[16] = {1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1};
uchar sum=0,paritc = 0;
uchar parit0c = 0, parit1c = 0, parit2c = 0, parit3c = 0;
uchar parit4c = 0, parit5c = 0, parit6c = 0, parit7c = 0;
uchar parit1_1, parit1_2, parit2_1, parit2_2, parit4_1, parit4_2;
uchar parit8_1 = 0, parit8_2 = 0, parit16_1 = 0, parit16_2 = 0, parit32_1 = 0, parit32_2 = 0;
uchar parit64_1 = 0, parit64_2 = 0, parit128_1 = 0, parit128_2 = 0, parit256_1 = 0, parit256_2 = 0;
uchar parit512_1 = 0, parit512_2 = 0, parit1024_1 = 0, parit1024_2 = 0;
if(argc!=2){
printf("takes one argument which is data file name\n");
exit(1);
}
//strcpy(inp_file,argv[2]);
get_data_gen(argv[1],write_data); /* Get DATA for calculating */
for( i = 0; i < 256; i++)
{
paritc = paritc ^ (*(write_data+i));
tmp = (*(write_data+i) & 0xf0) >> 4;
tmp2 = *(write_data+i) & 0x0f;
switch(tmp)
{
case 0:
*(paritr + i) = *(data_table0 + tmp2);
break;
case 1:
*(paritr + i) = *(data_table1 + tmp2);
break;
case 2:
*(paritr + i) = *(data_table1 + tmp2);
break;
case 3:
*(paritr + i) = *(data_table0 + tmp2);
break;
case 4:
*(paritr + i) = *(data_table1 + tmp2);
break;
case 5:
*(paritr + i) = *(data_table0 + tmp2);
break;
case 6:
*(paritr + i) = *(data_table0 + tmp2);
break;
case 7:
*(paritr + i) = *(data_table1 + tmp2);
break;
case 8:
*(paritr + i) = *(data_table1 + tmp2);
break;
case 9:
*(paritr + i) = *(data_table0 + tmp2);
break;
case 10:
*(paritr + i) = *(data_table0 + tmp2);
break;
case 11:
*(paritr + i) = *(data_table1 + tmp2);
break;
case 12:
*(paritr + i) = *(data_table0 + tmp2);
break;
case 13:
*(paritr + i) = *(data_table1 + tmp2);
break;
case 14:
*(paritr + i) = *(data_table1 + tmp2);
break;
case 15:
*(paritr + i) = *(data_table0 + tmp2);
break;
}
}
parit0c = ((paritc & 0x01) ? 1 : 0);
parit1c = ((paritc & 0x02) ? 1 : 0);
parit2c = ((paritc & 0x04) ? 1 : 0);
parit3c = ((paritc & 0x08) ? 1 : 0);
parit4c = ((paritc & 0x10) ? 1 : 0);
parit5c = ((paritc & 0x20) ? 1 : 0);
parit6c = ((paritc & 0x40) ? 1 : 0);
parit7c = ((paritc & 0x80) ? 1 : 0);
parit1_2 = parit6c ^ parit4c ^ parit2c ^ parit0c;
parit1_1 = parit7c ^ parit5c ^ parit3c ^ parit1c;
parit2_2 = parit5c ^ parit4c ^ parit1c ^ parit0c;
parit2_1 = parit7c ^ parit6c ^ parit3c ^ parit2c;
parit4_2 = parit3c ^ parit2c ^ parit1c ^ parit0c;
parit4_1 = parit7c ^ parit6c ^ parit5c ^ parit4c;
for( i = 0 ; i < 256;i++) sum=sum ^ (*(paritr+i));
for ( i = 0; i < 256; i = i+2 )
parit8_2 = parit8_2 ^ (*(paritr + i));
for ( i = 0; i < 256; i = i+4 )
{
parit16_2 = parit16_2 ^ (*(paritr + i));
parit16_2 = parit16_2 ^ (*(paritr + i + 1));
}
for ( i = 0; i < 256; i = i+8 )
{
for ( j = 0; j <= 3; j++ )
parit32_2 = parit32_2 ^ (*(paritr + i + j));
}
for ( i = 0; i < 256; i = i+16 )
{
for ( j = 0; j <= 7; j++ )
parit64_2 = parit64_2 ^ (*(paritr + i + j));
}
for ( i = 0; i < 256; i = i+32 )
{
for ( j = 0; j <= 15; j++ )
parit128_2 = parit128_2 ^ (*(paritr + i + j));
}
for ( i = 0; i < 256; i = i+64 )
{
for ( j = 0; j <= 31; j++ )
parit256_2 = parit256_2 ^ (*(paritr + i + j));
}
for ( i = 0; i < 256; i = i+128 )
{
for ( j = 0; j <= 63; j++ )
parit512_2 = parit512_2 ^ (*(paritr + i + j));
}
for ( i = 0; i < 256; i = i+256 )
{
for ( j = 0; j <= 127; j++ )
parit1024_2 = parit1024_2 ^ (*(paritr + i + j));
}
if(sum==0){
parit1024_1=parit1024_2;
parit512_1=parit512_2;
parit256_1=parit256_2;
parit128_1=parit128_2;
parit64_1=parit64_2;
parit32_1=parit32_2;
parit16_1=parit16_2;
parit8_1=parit8_2;
}
else{
parit1024_1 = (parit1024_2 ? 0 : 1);
parit512_1 = (parit512_2 ? 0 : 1);
parit256_1 = (parit256_2 ? 0 : 1);
parit128_1 = (parit128_2 ? 0 : 1);
parit64_1 = (parit64_2 ? 0 : 1);
parit32_1 = (parit32_2 ? 0 : 1);
parit16_1 = (parit16_2 ? 0 : 1);
parit8_1 = (parit8_2 ? 0 : 1);
}
parit1_2 <<= 2;
parit1_1 <<= 3;
parit2_2 <<= 4;
parit2_1 <<= 5;
parit4_2 <<= 6;
parit4_1 <<= 7;
parit128_1 <<= 1;
parit256_2 <<= 2;
parit256_1 <<= 3;
parit512_2 <<= 4;
parit512_1 <<= 5;
parit1024_2 <<= 6;
parit1024_1 <<= 7;
parit8_1 <<= 1;
parit16_2 <<= 2;
parit16_1 <<= 3;
parit32_2 <<= 4;
parit32_1 <<= 5;
parit64_2 <<= 6;
parit64_1 <<= 7;
*(ecc_gen + 0) = ~( parit64_1|parit64_2|parit32_1|parit32_2|parit16_1|parit16_2|parit8_1|parit8_2 );
*(ecc_gen + 1) = ~( parit1024_1|parit1024_2|parit512_1|parit512_2|parit256_1|parit256_2|parit128_1|parit128_2 );
*(ecc_gen + 2) = ~( parit4_1|parit4_2|parit2_1|parit2_2|parit1_1|parit1_2|0x00|0x00 );
printf("Result %02X %02X %02X\n",ecc_gen[0],ecc_gen[1],ecc_gen[2]);
}