-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.c
135 lines (107 loc) · 3.05 KB
/
test.c
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
#include <string.h>
#include "Headers/op.h"
#include "Headers/array.h"
#include "Headers/encode.h"
#include "Headers/decode.h"
#define BUFFER_SIZE 100
int main()
{
struct gf_tables *gf_table = malloc(sizeof(struct gf_tables));
gf_table->gf_exp = malloc(sizeof(struct Array));
gf_table->gf_log = malloc(sizeof(struct Array));
initArray(gf_table->gf_exp, 512);
initArray(gf_table->gf_log, 256);
gf_table = init_tables();
printf("##### Reed-Solomon Error Correction #####\n");
printf("Enter the string you want to test and press enter when you're done:\n");
struct Array *msg_in = malloc(sizeof(struct Array));
initArray(msg_in, 50);
char my_msg[BUFFER_SIZE];
fgets( my_msg, sizeof(my_msg), stdin );
for (size_t i = 0; i < strlen(my_msg); i++) {
msg_in->array[i] = (int)my_msg[i];
insertArray(msg_in);
}
printf("Msg in: [");
for (size_t i = 0; i < msg_in->used; i++) {
printf("%u,",msg_in->array[i]);
}
printf("]\n");
struct Array *msg = malloc(sizeof(struct Array));
initArray(msg, 170);
struct Array *err_loc = malloc(sizeof(struct Array));
struct Array *synd = malloc(sizeof(struct Array));
struct Array *pos = malloc(sizeof(struct Array));
struct Array *rev_pos = malloc(sizeof(struct Array));
msg = rs_encode_msg(msg_in, 14, gf_table);
printf("Msg Encoded: [");
for (size_t i = 0; i < msg->used; i++) {
printf("%u,",msg->array[i]);
}
printf("]\n");
//Tempering msg
msg->array[0] = 0;
msg->array[3] = 0;
msg->array[10] = 0;
printf("Msg Tempered: [");
for (size_t i = 0; i < strlen(my_msg); i++) {
printf("%u,",msg->array[i]);
}
printf("]\n");
printf("Msg Tempered: ");
for (size_t i = 0; i < strlen(my_msg); i++) {
printf("%c",msg->array[i]);
}
printf("\n");
printf("Msg Encoded: [");
for (size_t i = 0; i < msg->used; i++) {
printf("%u,",msg->array[i]);
}
printf("]\n");
synd = rs_calc_syndromes(msg, 14, gf_table);
printf("synd : ");
for (size_t i = 0; i < synd->used; i++) {
printf("%u, ",synd->array[i]);
}
printf("\n");
err_loc = rs_find_error_locator(synd, 14, 0, gf_table);
printf("err_loc : ");
for (size_t i = 0; i < err_loc->used; i++) {
printf("%u, ",err_loc->array[i]);
}
printf("\n");
pos = rs_find_errors(reverse_arr(err_loc), msg->used, gf_table);
printf("err_pos : ");
for (size_t i = 0; i < pos->used; i++) {
printf("%u, ",pos->array[i]);
}
printf("\n");
rev_pos = reverse_arr(pos);
printf("Error positions: [");
for (size_t i = 0; i < rev_pos->used; i++) {
printf("%u,", rev_pos->array[i]);
}
printf("]\n");
struct Array *err_pos = malloc(sizeof(struct Array));
initArray(err_pos, 3);
err_pos->array[0] = 0;
msg = rs_correct_msg(msg, 14, err_pos, gf_table);
printf("Msg Corrected: [");
for (size_t i = 0; i < msg->used; i++) {
printf("%u,",msg->array[i]);
}
printf("]\n");
printf("Msg Corrected: ");
for (size_t i = 0; i < strlen(my_msg); i++) {
printf("%c",msg->array[i]);
}
printf("\n");
freeArray(gf_table->gf_exp);
freeArray(gf_table->gf_log);
freeArray(msg_in);
freeArray(msg);
freeArray(synd);
freeArray(pos);
freeArray(rev_pos);
return 0;
}