test_pi_ecc.c
3.33 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
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "pi_ecc.h"
/*
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 */
}
/*
Reading DATA file
*/
get_data_cor(unsigned char *file_name,
unsigned char *data,
unsigned char *ecc
)
{
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;
}
for(i=0;i<3;i++){
fscanf(fdata,"%x",&j); /* Reading DATA */
ecc[i]=(unsigned char)j;
}
fclose(fdata); /* Close DATA file */
}
put_data(unsigned char *data,unsigned char *ecc)
{
int i,j;
for(i=0; i<16; ++i) {
for(j=0; j<16; ++j)
printf("%02x ",data[i*16+j]); /* Wrinting DATA */
printf("\n");
}
printf("%02x %02x %02x",ecc[0],ecc[1],ecc[2]); /* Writing ECC code*/
}
main(int argc,char *argv[])
{
int a;
unsigned char *data; /* DATA */
unsigned char inp_file[1024]; /* File name */
unsigned char ecc[3];
/* ecc[0]: LP15,LP14,LP13,... */
/* ecc[1]: LP07,LP06,LP05,... */
/* ecc[2]: CP5,CP4,CP3,...,"1","1" */
if(argc<3){
printf("usage: test_pi_ecc <mode> <input_data_filename>\n");
printf(" where <mode>:\n g --> generate\n c --> correct\n");
printf(" <input_data_filename> contents such as cor_in_ex.dat.\n");
printf(" The last 3 bytes of the file are output when the generate\n");
printf(" option 'g' is used, and are not needed for 'c' (correct).\n");
return;
}
/* DATA file default 'eec.dat' */
strcpy(inp_file,argv[2]);
data=malloc(256); /* For DATA */
if (data==0) { /* If no memory */
printf("Insufficient memory!\n");
exit(-1);
}
if(argv[1][0]=='g'){
get_data_gen(inp_file,data); /* Get DATA for calculating */
calculate_ecc(data,ecc); /* Calculating ECC code */
printf("Result %02x %02x %02x\n",ecc[0],ecc[1],ecc[2]);
}
else if(argv[1][0]=='c'){
get_data_cor(inp_file,data,ecc); /* Get DATA for calculating */
a = correct_ecc(data,ecc);
printf("Result: ");
switch(a) {
case 0: printf("No error"); break;
case 1: printf("Corrected\n"); put_data(data,ecc);break;
case 2: printf("ECC error\n"); put_data(data,ecc);break;
case 3: printf("Uncorrectable"); break;
default: printf("Unknown error"); break;
}
printf("\n");
}
else
printf("bad mode argument\n");
}