forked from quiet/libfec
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rstest.c
296 lines (253 loc) · 7.27 KB
/
rstest.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Test the Reed-Solomon codecs
* for various block sizes and with random data and random error patterns
*
* Copyright 2002 Phil Karn, KA9Q
* May be used under the terms of the GNU Lesser General Public License (LGPL)
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <time.h>
#include "fec.h"
struct etab {
int symsize;
int genpoly;
int fcs;
int prim;
int nroots;
int ntrials;
} Tab[] = {
{2, 0x7, 1, 1, 1, 10 },
{3, 0xb, 1, 1, 2, 10 },
{4, 0x13, 1, 1, 4, 10 },
{5, 0x25, 1, 1, 6, 10 },
{6, 0x43, 1, 1, 8, 10 },
{7, 0x89, 1, 1, 10, 10 },
{8, 0x11d, 1, 1, 32, 10 },
{8, 0x187, 112,11, 32, 10 }, /* Duplicates CCSDS codec */
{9, 0x211, 1, 1, 32, 10 },
{10,0x409, 1, 1, 32, 10 },
{11,0x805, 1, 1, 32, 10 },
{12,0x1053, 1, 1, 32, 5 },
{13,0x201b, 1, 1, 32, 2 },
{14,0x4443, 1, 1, 32, 1 },
{15,0x8003, 1, 1, 32, 1 },
{16,0x1100b, 1, 1, 32, 1 },
{0, 0, 0, 0, 0},
};
int exercise_char(struct etab *e);
int exercise_int(struct etab *e);
int exercise_8(void);
int main(){
int i;
srandom(time(NULL));
printf("Testing fixed CCSDS encoder...\n");
exercise_8();
for(i=0;Tab[i].symsize != 0;i++){
int nn,kk;
nn = (1<<Tab[i].symsize) - 1;
kk = nn - Tab[i].nroots;
printf("Testing (%d,%d) code...\n",nn,kk);
if(Tab[i].symsize <= 8)
exercise_char(&Tab[i]);
else
exercise_int(&Tab[i]);
}
exit(0);
}
int exercise_8(void){
int nn = 255;
unsigned char block[nn],tblock[nn];
int errlocs[nn],derrlocs[nn];
int i;
int errors;
int derrors,kk;
int errval,errloc;
int erasures;
int decoder_errors = 0;
/* Compute code parameters */
kk = 223;
/* Test up to the error correction capacity of the code */
for(errors=0;errors<=(nn-kk)/2;errors++){
/* Load block with random data and encode */
for(i=0;i<kk;i++)
block[i] = random() & nn;
memcpy(tblock,block,sizeof(block));
encode_rs_8(block,&block[kk],0);
/* Make temp copy, seed with errors */
memcpy(tblock,block,sizeof(block));
memset(errlocs,0,sizeof(errlocs));
memset(derrlocs,0,sizeof(derrlocs));
erasures=0;
for(i=0;i<errors;i++){
do {
errval = random() & nn;
} while(errval == 0); /* Error value must be nonzero */
do {
errloc = random() % nn;
} while(errlocs[errloc] != 0); /* Must not choose the same location twice */
errlocs[errloc] = 1;
#if FLAG_ERASURE
if(random() & 1) /* 50-50 chance */
derrlocs[erasures++] = errloc;
#endif
tblock[errloc] ^= errval;
}
/* Decode the errored block */
derrors = decode_rs_8(tblock,derrlocs,erasures,0);
if(derrors != errors){
printf("(%d,%d) decoder says %d errors, true number is %d\n",nn,kk,derrors,errors);
decoder_errors++;
}
for(i=0;i<derrors;i++){
if(errlocs[derrlocs[i]] == 0){
printf("(%d,%d) decoder indicates error in location %d without error\n",nn,kk,derrlocs[i]);
decoder_errors++;
}
}
if(memcmp(tblock,block,sizeof(tblock)) != 0){
printf("(%d,%d) decoder uncorrected errors! output ^ input:",nn,kk);
decoder_errors++;
for(i=0;i<nn;i++)
printf(" %02x",tblock[i] ^ block[i]);
printf("\n");
}
}
return decoder_errors;
}
int exercise_char(struct etab *e){
int nn = (1<<e->symsize) - 1;
unsigned char block[nn],tblock[nn];
int errlocs[nn],derrlocs[nn];
int i;
int errors;
int derrors,kk;
int errval,errloc;
int erasures;
int decoder_errors = 0;
void *rs;
if(e->symsize > 8)
return -1;
/* Compute code parameters */
kk = nn - e->nroots;
rs = init_rs_char(e->symsize,e->genpoly,e->fcs,e->prim,e->nroots,0);
if(rs == NULL){
printf("init_rs_char failed!\n");
return -1;
}
/* Test up to the error correction capacity of the code */
for(errors=0;errors <= e->nroots/2;errors++){
/* Load block with random data and encode */
for(i=0;i<kk;i++)
block[i] = random() & nn;
memcpy(tblock,block,sizeof(block));
encode_rs_char(rs,block,&block[kk]);
/* Make temp copy, seed with errors */
memcpy(tblock,block,sizeof(block));
memset(errlocs,0,sizeof(errlocs));
memset(derrlocs,0,sizeof(derrlocs));
erasures=0;
for(i=0;i<errors;i++){
do {
errval = random() & nn;
} while(errval == 0); /* Error value must be nonzero */
do {
errloc = random() % nn;
} while(errlocs[errloc] != 0); /* Must not choose the same location twice */
errlocs[errloc] = 1;
#if FLAG_ERASURE
if(random() & 1) /* 50-50 chance */
derrlocs[erasures++] = errloc;
#endif
tblock[errloc] ^= errval;
}
/* Decode the errored block */
derrors = decode_rs_char(rs,tblock,derrlocs,erasures);
if(derrors != errors){
printf("(%d,%d) decoder says %d errors, true number is %d\n",nn,kk,derrors,errors);
decoder_errors++;
}
for(i=0;i<derrors;i++){
if(errlocs[derrlocs[i]] == 0){
printf("(%d,%d) decoder indicates error in location %d without error\n",nn,kk,derrlocs[i]);
decoder_errors++;
}
}
if(memcmp(tblock,block,sizeof(tblock)) != 0){
printf("(%d,%d) decoder uncorrected errors! output ^ input:",nn,kk);
decoder_errors++;
for(i=0;i<nn;i++)
printf(" %02x",tblock[i] ^ block[i]);
printf("\n");
}
}
free_rs_char(rs);
return 0;
}
int exercise_int(struct etab *e){
int nn = (1<<e->symsize) - 1;
int block[nn],tblock[nn];
int errlocs[nn],derrlocs[nn];
int i;
int errors;
int derrors,kk;
int errval,errloc;
int erasures;
int decoder_errors = 0;
void *rs;
/* Compute code parameters */
kk = nn - e->nroots;
rs = init_rs_int(e->symsize,e->genpoly,e->fcs,e->prim,e->nroots,0);
if(rs == NULL){
printf("init_rs_int failed!\n");
return -1;
}
/* Test up to the error correction capacity of the code */
for(errors=0;errors <= e->nroots/2;errors++){
/* Load block with random data and encode */
for(i=0;i<kk;i++)
block[i] = random() & nn;
memcpy(tblock,block,sizeof(block));
encode_rs_int(rs,block,&block[kk]);
/* Make temp copy, seed with errors */
memcpy(tblock,block,sizeof(block));
memset(errlocs,0,sizeof(errlocs));
memset(derrlocs,0,sizeof(derrlocs));
erasures=0;
for(i=0;i<errors;i++){
do {
errval = random() & nn;
} while(errval == 0); /* Error value must be nonzero */
do {
errloc = random() % nn;
} while(errlocs[errloc] != 0); /* Must not choose the same location twice */
errlocs[errloc] = 1;
#if FLAG_ERASURE
if(random() & 1) /* 50-50 chance */
derrlocs[erasures++] = errloc;
#endif
tblock[errloc] ^= errval;
}
/* Decode the errored block */
derrors = decode_rs_int(rs,tblock,derrlocs,erasures);
if(derrors != errors){
printf("(%d,%d) decoder says %d errors, true number is %d\n",nn,kk,derrors,errors);
decoder_errors++;
}
for(i=0;i<derrors;i++){
if(errlocs[derrlocs[i]] == 0){
printf("(%d,%d) decoder indicates error in location %d without error\n",nn,kk,derrlocs[i]);
decoder_errors++;
}
}
if(memcmp(tblock,block,sizeof(tblock)) != 0){
printf("(%d,%d) decoder uncorrected errors! output ^ input:",nn,kk);
decoder_errors++;
for(i=0;i<nn;i++)
printf(" %02x",tblock[i] ^ block[i]);
printf("\n");
}
}
free_rs_int(rs);
return 0;
}