2 * Reed Solomon Encoder/Decoder
4 * Copyright Henry Minsky (hqm@alum.mit.edu) 1991-2009
6 * This software library is licensed under terms of the GNU GENERAL
9 * RSCODE is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * RSCODE is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Rscode. If not, see <http://www.gnu.org/licenses/>.
22 * Commercial licensing is available under a separate license, please
23 * contact author for details.
25 * Source code is available at http://rscode.sourceforge.net
32 /* Encoder parity bytes */
35 /* Decoder syndrome bytes */
38 /* generator polynomial */
39 int genPoly
[MAXDEG
*2];
44 compute_genpoly (int nbytes
, int genpoly
[]);
46 /* Initialize lookup tables, polynomials, etc. */
50 /* Initialize the galois field arithmetic tables */
53 /* Compute the encoder generator polynomial */
54 compute_genpoly(RS_ECC_NPARITY
, genPoly
);
58 zero_fill_from (unsigned char buf
[], int from
, int to
)
61 for (i
= from
; i
< to
; i
++) buf
[i
] = 0;
64 /* debugging routines */
70 printf("Parity Bytes: ");
71 for (i
= 0; i
< RS_ECC_NPARITY
; i
++)
72 printf("[%d]:%x, ",i
,pBytes
[i
]);
83 printf("Syndrome Bytes: ");
84 for (i
= 0; i
< RS_ECC_NPARITY
; i
++)
85 printf("[%d]:%x, ",i
,synBytes
[i
]);
90 /* Append the parity bytes onto the end of the message */
92 build_codeword (unsigned char msg
[], int nbytes
, unsigned char dst
[])
96 for (i
= 0; i
< nbytes
; i
++) dst
[i
] = msg
[i
];
98 for (i
= 0; i
< RS_ECC_NPARITY
; i
++) {
99 dst
[i
+nbytes
] = pBytes
[RS_ECC_NPARITY
-1-i
];
103 /**********************************************************
104 * Reed Solomon Decoder
106 * Computes the syndrome of a codeword. Puts the results
107 * into the synBytes[] array.
111 decode_data(unsigned char data
[], int nbytes
)
114 for (j
= 0; j
< RS_ECC_NPARITY
; j
++) {
116 for (i
= 0; i
< nbytes
; i
++) {
117 sum
= data
[i
] ^ gmult(gexp
[j
+1], sum
);
124 /* Check if the syndrome is zero */
126 check_syndrome (void)
129 for (i
=0 ; i
< RS_ECC_NPARITY
; i
++) {
130 if (synBytes
[i
] != 0) {
140 debug_check_syndrome (void)
145 for (i
= 0; i
< 3; i
++) {
146 printf(" inv log S[%d]/S[%d] = %d\n", i
, i
+1,
147 glog
[gmult(synBytes
[i
], ginv(synBytes
[i
+1]))]);
153 /* Create a generator polynomial for an n byte RS code.
154 * The coefficients are returned in the genPoly arg.
155 * Make sure that the genPoly array which is passed in is
156 * at least n+1 bytes long.
160 compute_genpoly (int nbytes
, int genpoly
[])
162 int i
, tp
[MAXDEG
], tp1
[MAXDEG
];
164 /* multiply (x + a^n) for n = 1 to nbytes */
169 for (i
= 1; i
<= nbytes
; i
++) {
171 tp
[0] = gexp
[i
]; /* set up x+a^n */
174 mult_polys(genpoly
, tp
, tp1
);
175 copy_poly(tp1
, genpoly
);
179 /* Simulate a LFSR with generator polynomial for n byte RS code.
180 * Pass in a pointer to the data array, and amount of data.
182 * The parity bytes are deposited into pBytes[], and the whole message
183 * and parity are copied to dest to make a codeword.
188 encode_data (unsigned char msg
[], int nbytes
, unsigned char dst
[])
190 int i
, LFSR
[RS_ECC_NPARITY
+1],dbyte
, j
;
192 for(i
=0; i
< RS_ECC_NPARITY
+1; i
++) LFSR
[i
]=0;
194 for (i
= 0; i
< nbytes
; i
++) {
195 dbyte
= msg
[i
] ^ LFSR
[RS_ECC_NPARITY
-1];
196 for (j
= RS_ECC_NPARITY
-1; j
> 0; j
--) {
197 LFSR
[j
] = LFSR
[j
-1] ^ gmult(genPoly
[j
], dbyte
);
199 LFSR
[0] = gmult(genPoly
[0], dbyte
);
202 for (i
= 0; i
< RS_ECC_NPARITY
; i
++)
205 build_codeword(msg
, nbytes
, dst
);