75
75
#include <string.h>
76
76
#include "bch.h"
77
77
78
- #undef DEBUG
79
-
80
78
int init_bch (bch_t * bch , int m , int length , int t ) {
81
79
82
80
int p [21 ], n ;
@@ -119,16 +117,16 @@ int init_bch(bch_t *bch, int m, int length, int t) {
119
117
else if (m == 20 ) p [3 ] = 1 ;
120
118
121
119
n = 1 ;
122
- #ifdef DEBUG
120
+ #ifdef BCH_DEBUG
123
121
printf ("p(x) = " );
124
122
#endif
125
123
for (int i = 0 ; i <= m ; i ++ ) {
126
124
n *= 2 ;
127
- #ifdef DEBUG
125
+ #ifdef BCH_DEBUG
128
126
printf ("%1d" , p [i ]);
129
127
#endif
130
128
}
131
- #ifdef DEBUG
129
+ #ifdef BCH_DEBUG
132
130
printf ("\n" );
133
131
#endif
134
132
n = n / 2 - 1 ;
@@ -192,7 +190,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
192
190
cycle [1 ][0 ] = 1 ;
193
191
size [1 ] = 1 ;
194
192
jj = 1 ; /* cycle set index */
195
- #ifdef DEBUG
193
+ #ifdef BCH_DEBUG
196
194
if (bch -> m > 9 ) {
197
195
printf ("Computing cycle sets modulo %d\n" , bch -> n );
198
196
printf ("(This may take some time)...\n" );
@@ -260,7 +258,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
260
258
return -4 ;
261
259
}
262
260
263
- #ifdef DEBUG
261
+ #ifdef BCH_DEBUG
264
262
printf ("This is a (%d, %d, %d) binary BCH code\n" , bch -> length , bch -> k , d );
265
263
#endif
266
264
@@ -277,7 +275,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
277
275
bch -> g [jj ] = bch -> g [jj - 1 ];
278
276
bch -> g [0 ] = bch -> alpha_to [(bch -> index_of [bch -> g [0 ]] + zeros [ii ]) % bch -> n ];
279
277
}
280
- #ifdef DEBUG
278
+ #ifdef BCH_DEBUG
281
279
printf ("Generator polynomial:\ng(x) = " );
282
280
for (ii = 0 ; ii <= rdncy ; ii ++ ) {
283
281
printf ("%d" , bch -> g [ii ]);
@@ -287,7 +285,7 @@ int init_bch(bch_t *bch, int m, int length, int t) {
287
285
return 0 ;
288
286
}
289
287
290
- void generate_bch (bch_t * bch , int * data , int * bb ) {
288
+ void generate_bch (bch_t * bch , const int * data , int * bb ) {
291
289
/*
292
290
* Compute redundacy bb[], the coefficients of b(x). The redundancy
293
291
* polynomial b(x) is the remainder after dividing x^(length-k)*data(x)
@@ -315,7 +313,7 @@ void generate_bch(bch_t *bch, int *data, int *bb) {
315
313
}
316
314
317
315
318
- int apply_bch (bch_t * bch , int * recd )
316
+ int apply_bch (const bch_t * bch , int * recd )
319
317
/*
320
318
* Simon Rockliff's implementation of Berlekamp's algorithm.
321
319
*
@@ -346,7 +344,7 @@ int apply_bch(bch_t *bch, int *recd)
346
344
t2 = 2 * bch -> t ;
347
345
348
346
/* first form the syndromes */
349
- #ifdef DEBUG
347
+ #ifdef BCH_DEBUG
350
348
printf ("S(x) = " );
351
349
#endif
352
350
for (i = 1 ; i <= t2 ; i ++ ) {
@@ -362,11 +360,11 @@ int apply_bch(bch_t *bch, int *recd)
362
360
*/
363
361
/* convert syndrome from polynomial form to index form */
364
362
s [i ] = bch -> index_of [s [i ]];
365
- #ifdef DEBUG
363
+ #ifdef BCH_DEBUG
366
364
printf ("%3d " , s [i ]);
367
365
#endif
368
366
}
369
- #ifdef DEBUG
367
+ #ifdef BCH_DEBUG
370
368
printf ("\n" );
371
369
#endif
372
370
@@ -468,7 +466,7 @@ int apply_bch(bch_t *bch, int *recd)
468
466
for (i = 0 ; i <= l [u ]; i ++ )
469
467
elp [u ][i ] = bch -> index_of [elp [u ][i ]];
470
468
471
- #ifdef DEBUG
469
+ #ifdef BCH_DEBUG
472
470
printf ("sigma(x) = " );
473
471
for (i = 0 ; i <= l [u ]; i ++ )
474
472
printf ("%3d " , elp [u ][i ]);
@@ -491,12 +489,12 @@ int apply_bch(bch_t *bch, int *recd)
491
489
root [count ] = i ;
492
490
loc [count ] = bch -> n - i ;
493
491
count ++ ;
494
- #ifdef DEBUG
492
+ #ifdef BCH_DEBUG
495
493
printf ("%3d " , bch -> n - i );
496
494
#endif
497
495
}
498
496
}
499
- #ifdef DEBUG
497
+ #ifdef BCH_DEBUG
500
498
printf ("\n" );
501
499
#endif
502
500
if (count == l [u ]) {
@@ -506,7 +504,7 @@ int apply_bch(bch_t *bch, int *recd)
506
504
return l [u ];
507
505
}
508
506
else { /* elp has degree >t hence cannot solve */
509
- #ifdef DEBUG
507
+ #ifdef BCH_DEBUG
510
508
printf ("Incomplete decoding: errors detected\n" );
511
509
#endif
512
510
return -1 ;
@@ -520,7 +518,7 @@ int apply_bch(bch_t *bch, int *recd)
520
518
}
521
519
522
520
/* LEFT justified in hex */
523
- void bytes_to_bits (int * bytes , int * bit_dest , int num_bits ) {
521
+ void bytes_to_bits (const int * bytes , int * bit_dest , int num_bits ) {
524
522
for (int i = 0 ; i < num_bits ; i ++ ) {
525
523
int index = i / 8 ;
526
524
int bit_pos = 7 - (i % 8 );
@@ -529,7 +527,7 @@ void bytes_to_bits(int *bytes, int *bit_dest, int num_bits) {
529
527
}
530
528
}
531
529
532
- void bits_to_bytes (int * bits , int * byte_dest , int num_bits ) {
530
+ void bits_to_bytes (const int * bits , int * byte_dest , int num_bits ) {
533
531
534
532
int index ;
535
533
@@ -540,30 +538,66 @@ void bits_to_bytes(int *bits, int *byte_dest, int num_bits) {
540
538
}
541
539
542
540
byte_dest [index ] <<= 1 ;
543
- byte_dest [index ] |= bits [i ] & 0x01 ;
541
+ byte_dest [index ] |= ( bits [i ] & 0x01 ) ;
544
542
}
545
543
546
544
byte_dest [index ] <<= 8 - (num_bits % 8 );
547
545
}
548
546
549
- void swap_format (int * bits , int cutoff , int num_bits ) {
547
+ void swap_format (const int * bits , int * dest , int cutoff , int num_bits ) {
550
548
// Do it the easy way
551
- int * temp = malloc (num_bits * sizeof (int ));
552
549
for (int i = 0 ; i < num_bits ; i ++ ) {
553
550
if (i < cutoff ) {
554
- temp [num_bits - cutoff + i ] = bits [i ];
551
+ dest [num_bits - cutoff + i ] = bits [i ];
555
552
} else {
556
- temp [i - cutoff ] = bits [i ];
553
+ dest [i - cutoff ] = bits [i ];
557
554
}
558
555
}
556
+ }
557
+
558
+ int rotate_byte (int x ) {
559
+ int y = 0 ;
560
+
561
+ for (int i = 0 ; i < 8 ; i ++ ) {
562
+ y <<= 1 ;
563
+ y |= (x & 0x01 );
564
+ x >>= 1 ;
565
+ }
559
566
560
- memcpy ( bits , temp , num_bits * sizeof ( int )) ;
567
+ return y ;
561
568
}
562
569
563
- void dump_bch (bch_t * bch ) {
570
+ void rotate_bits (const int * in , int * out , int num_bits ) {
571
+ for (int i = 0 ; i < num_bits ; i ++ ) {
572
+ out [i ] = in [num_bits - i - 1 ];
573
+ }
574
+ }
575
+
576
+ void invert_bits (const int * bits , int * dest , int num_bits ) {
577
+ for (int i = 0 ; i < num_bits ; i ++ ) {
578
+ dest [i ] = (bits [i ] == 0 );
579
+ }
580
+ }
581
+
582
+ void dump_bch (const bch_t * bch ) {
564
583
printf ("m: %d length: %d t: %d n: %d k: %d\n" , bch -> m , bch -> length , bch -> t , bch -> n , bch -> k );
565
584
}
566
585
586
+ void print_array (const char * msg , const char * format , const int * bytes , int num_bytes ) {
587
+ printf ("%s" , msg );
588
+ for (int i = 0 ; i < num_bytes ; i ++ ) {
589
+ printf (format , bytes [i ]);
590
+ }
591
+ }
592
+
593
+ void print_bytes (const char * msg , const int * bytes , int num_bytes ) {
594
+ print_array (msg , "%02x " , bytes , num_bytes );
595
+ }
596
+
597
+ void print_bits (const char * msg , const int * bits , int num_bits ) {
598
+ print_array (msg , "%d " , bits , num_bits );
599
+ }
600
+
567
601
#undef MAIN
568
602
#undef TEST_BYTES_TO_BITS
569
603
#define TEST_SWAP
0 commit comments