-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
executable file
·180 lines (145 loc) · 3.55 KB
/
test.c
File metadata and controls
executable file
·180 lines (145 loc) · 3.55 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <time.h>
#include "libbch.h"
void dump_data(uint8_t *data, unsigned int len)
{
int i;
for (i = 0; i < len; i++) {
if ((i % 8) == 0)
printf("\n");
printf("0x%02X ", data[i]);
}
printf("\n");
}
void parse_bch_decode_err(int err_code)
{
if (err_code == -EBADMSG)
printf("libbch decode failed: %d\n", err_code);
else if (err_code == -EINVAL)
printf("bhclib decode invalid parameters: %d\n", err_code);
else
printf("bhclib decode unknown error: %d\n", err_code);
}
int simple_test(void)
{
struct libbch *libbch;
uint8_t *data;
uint8_t *ecc;
int errcnt;
libbch = libbch_init(512, 8);
libbch_dump(libbch);
data = malloc(libbch->data_len);
memset(data, 0xFF, libbch->data_len);
ecc = malloc(libbch->ecc_len);
memset(ecc, 0x0, libbch->ecc_len);
libbch_encode(libbch, data, ecc);
//dump_data(data, libbch->data_len);
//dump_data(ecc, libbch->ecc_len);
data[0] = 0xF0;
ecc[0] = ecc[0] ^ 0xF0;
errcnt = libbch_decode(libbch, data, ecc);
if (errcnt > 0) {
libbch_dump_errloc(libbch);
} else if (errcnt < 0) {
parse_bch_decode_err(errcnt);
goto has_errors;
}
libbch_correct_all(libbch, data, ecc);
errcnt = libbch_decode(libbch, data, ecc);
if (errcnt > 0) {
libbch_dump_errloc(libbch);
} else if (errcnt < 0) {
parse_bch_decode_err(errcnt);
goto has_errors;
}
return 0;
has_errors:
libbch_free(libbch);
return errcnt;
}
void init_random_bch_data(struct libbch *libbch, uint8_t *data, uint8_t *ecc)
{
int i;
uint32_t *data32 = (uint32_t*)data;
for (i = 0; i < (libbch->data_len / 4); i++)
data32[i] = rand();
memset(ecc, 0x0, libbch->ecc_len);
libbch_encode(libbch, data, ecc);
}
void flip_random_bits(struct libbch *libbch, uint8_t *data, uint8_t *ecc)
{
int i;
unsigned int data_bits = libbch->data_len * 8;
unsigned int ecc_bits = libbch->ecc_len * 8;
unsigned int total_bits = data_bits + ecc_bits;
unsigned int flip_bits = (rand() % libbch->ecc_cap) + 1;
unsigned int err_bit;
unsigned int err_byte;
for (i = 0; i < flip_bits; i++) {
err_bit = rand() % total_bits;
err_byte = err_bit / 8;
if (err_bit < data_bits) {
data[err_byte] ^= 1 << (err_bit % 8);
} else {
ecc[err_byte - libbch->data_len] ^= 1 << (err_bit % 8);
}
}
}
int libbch_verify_random(void)
{
struct libbch *libbch;
uint8_t *data;
uint8_t *ecc;
uint8_t *data_golden;
uint8_t *ecc_golden;
int errcnt;
libbch = libbch_init(64, 8);
libbch_dump(libbch);
data = malloc(libbch->data_len);
ecc = malloc(libbch->ecc_len);
data_golden = malloc(libbch->data_len);
ecc_golden = malloc(libbch->ecc_len);
while (1) {
init_random_bch_data(libbch, data, ecc);
memcpy(data_golden, data, libbch->data_len);
memcpy(ecc_golden, ecc, libbch->ecc_len);
flip_random_bits(libbch, data, ecc);
errcnt = libbch_decode(libbch, data, ecc);
if (errcnt > 0) {
//libbch_dump_errloc(libbch);
} else if (errcnt < 0) {
parse_bch_decode_err(errcnt);
goto has_errors;
}
libbch_correct_all(libbch, data, ecc);
if (memcmp(data, data_golden, libbch->data_len)) {
printf("verify: data memcmp failed\n");
break;
}
if (memcmp(ecc, ecc_golden, libbch->ecc_len)) {
printf("verify: ecc memcmp failed\n");
break;
}
errcnt = libbch_decode(libbch, data, ecc);
if (errcnt != 0) {
printf("verify: decode failed\n");
break;
}
printf(".");
}
return 0;
has_errors:
libbch_free(libbch);
return errcnt;
}
int main(int argc, const char *argv[])
{
srand(time(NULL));
simple_test();
//libbch_verify_random();
return 0;
}