6 #define mm 8 /* RS code over GF(2**mm) - change to suit */
7 #define n 256 /* n = size of the field */
8 #define nn 182 /* nn=2**mm -1 length of codeword */
9 #define kk 172 /* kk = nn-2*tt */ /* Degree of g(x) = 2*tt */
22 const int NROOTS = nn-kk;
23 const int PAD = (n-1)-nn;
29 #define min(a,b) ((a) < (b) ? (a) : (b))
32 /**** Primitive polynomial ****/
33 int pp [mm+1] = { 1, 0, 1, 1, 1, 0, 0, 0, 1}; /* 1+x^2+x^3+x^4+x^8 */
35 /* generator polynomial, tables for Galois field */
36 int alpha_to[n], index_of[n], gg[nn-kk+1];
40 /* data[] is the info vector, bb[] is the parity vector, recd[] is the
41 noise corrupted received vector */
42 int recd[nn], data[kk], bb[nn-kk];
47 x = (x >> 0xff) + (x & 0xff);
55 register int i, mask ;
60 { alpha_to[i] = mask ;
61 index_of[alpha_to[i]] = i ;
62 if (pp[i]!=0) /* If pp[i] == 1 then, term @^i occurs in poly-repr of @^mm */
63 alpha_to[mm] ^= mask ; /* Bit-wise EXOR operation */
64 mask <<= 1 ; /* single left-shift */
66 index_of[alpha_to[mm]] = mm ;
67 /* Have obtained poly-repr of @^mm. Poly-repr of @^(i+1) is given by
68 poly-repr of @^i shifted left one-bit and accounting for any @^mm
69 term that may occur when poly-repr of @^i is shifted. */
71 for (i=mm+1; i<255; i++)
72 { if (alpha_to[i-1] >= mask)
73 alpha_to[i] = alpha_to[mm] ^ ((alpha_to[i-1]^mask)<<1) ;
74 else alpha_to[i] = alpha_to[i-1]<<1 ;
75 index_of[alpha_to[i]] = i ;
77 index_of[0] = A0 ;//-1
82 /* Obtain the generator polynomial of the tt-error correcting, length */
84 register int i, j, root;
88 for (i = 0,root=0*1; i < nn-kk; i++,root += 1) {
91 for (j = i; j > 0; j--){
93 gg[j] = gg[j-1] ^ alpha_to[modnn(index_of[gg[j]] + root)];
98 gg[0] = alpha_to[modnn(index_of[gg[0]] + root)];
100 for (i=0; i <= nn-kk; i++) {
101 gg[i] = index_of[gg[i]];
106 void rs_encode(unsigned char *data, unsigned char *bb)
111 for (i=0; i<NROOTS; i++) bb[i] = 0; //nullify result
113 for(i=0;i<NN-NROOTS-PAD;i++){
114 feedback = index_of[data[i] ^ bb[0]];
116 if(feedback != A0){ /* feedback term is non-zero */
117 for(j=1;j<NROOTS;j++) {
118 bb[j] ^= alpha_to[modnn(feedback + gg[NROOTS-j])];
122 memmove(&bb[0],&bb[1], NROOTS-1);
123 //for (j=0; j<NROOTS-1; j++) bb[j] = bb[j+1];
126 bb[NROOTS-1] = alpha_to[modnn(feedback + gg[0])];
132 int rs_decode(unsigned char *data, int *eras_pos, int no_eras){
133 int deg_lambda, el, deg_omega;
135 unsigned char u,q,tmp,num1,num2,den,discr_r;
136 unsigned char lambda[_NROOTS+1], s[_NROOTS];
137 unsigned char b[_NROOTS+1], t[_NROOTS+1], omega[_NROOTS+1];
138 unsigned char root[_NROOTS], reg[_NROOTS+1], loc[_NROOTS];
139 int syn_error, count;
142 // form the syndromes; i.e., evaluate data(x) at roots of g(x)
143 for(i=0;i<NROOTS;i++)
146 for(j=1;j<NN-PAD;j++){
147 for(i=0;i<NROOTS;i++){
151 s[i] = data[j] ^ alpha_to[modnn(index_of[s[i]] + (FCR+i)*PRIM)];
156 // Convert syndromes to index form, checking for nonzero condition
158 for(i=0;i<NROOTS;i++){
160 s[i] = index_of[s[i]];
164 // if syndrome is zero, data[] is a codeword and there are no
165 // errors to correct. So return data[] unmodified
170 memset(&lambda[1],0,NROOTS*sizeof(lambda[0]));
174 /* Init lambda to be the erasure locator polynomial */
175 lambda[1] = alpha_to[modnn(PRIM*(NN-1-eras_pos[0]))];
176 for (i = 1; i < no_eras; i++) {
177 u = modnn(PRIM*(NN-1-eras_pos[i]));
178 for (j = i+1; j > 0; j--) {
179 tmp = index_of[lambda[j - 1]];
181 lambda[j] ^= alpha_to[modnn(u + tmp)];
185 for(i=0;i<NROOTS+1;i++)
186 b[i] = index_of[lambda[i]];
189 * Begin Berlekamp-Massey algorithm to determine error+erasure
195 while (++r <= NROOTS) { /* r is the step number */
196 /* Compute discrepancy at the r-th step in poly-form */
198 for (i = 0; i < r; i++){
199 if ((lambda[i] != 0) && (s[r-i-1] != A0)) {
200 discr_r ^= alpha_to[modnn(index_of[lambda[i]] + s[r-i-1])];
203 discr_r = index_of[discr_r]; /* Index form */
205 /* 2 lines below: B(x) <-- x*B(x) */
206 memmove(&b[1],b,NROOTS*sizeof(b[0]));
209 /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
211 for (i = 0 ; i < NROOTS; i++) {
213 t[i+1] = lambda[i+1] ^ alpha_to[modnn(discr_r + b[i])];
215 t[i+1] = lambda[i+1];
217 if (2 * el <= r + no_eras - 1) {
218 el = r + no_eras - el;
220 * 2 lines below: B(x) <-- inv(discr_r) *
223 for (i = 0; i <= NROOTS; i++)
224 b[i] = (lambda[i] == 0) ? A0 : modnn(index_of[lambda[i]] - discr_r + NN);
226 /* 2 lines below: B(x) <-- x*B(x) */
227 memmove(&b[1],b,NROOTS*sizeof(b[0]));
230 memcpy(lambda,t,(NROOTS+1)*sizeof(t[0]));
234 /* Convert lambda to index form and compute deg(lambda(x)) */
236 for(i=0;i<NROOTS+1;i++){
237 lambda[i] = index_of[lambda[i]];
241 /* Find roots of the error+erasure locator polynomial by Chien search */
242 memcpy(®[1],&lambda[1],NROOTS*sizeof(reg[0]));
243 count = 0; /* Number of roots of lambda(x) */
244 for (i = 1,k=IPRIM-1; i <= NN; i++,k = modnn(k+IPRIM)) {
245 q = 1; /* lambda[0] is always 0 */
246 for (j = deg_lambda; j > 0; j--){
248 reg[j] = modnn(reg[j] + j);
249 q ^= alpha_to[reg[j]];
253 continue; /* Not a root */
254 /* store root (index-form) and error location number */
257 /* If we've already found max possible roots,
258 * abort the search to save time
260 if(++count == deg_lambda)
264 if (deg_lambda != count) {
266 * deg(lambda) unequal to number of roots => uncorrectable
273 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
274 * x**NROOTS). in index form. Also find deg(omega).
276 deg_omega = deg_lambda-1;
277 for (i = 0; i <= deg_omega;i++){
279 for(j=i;j >= 0; j--){
280 if ((s[i - j] != A0) && (lambda[j] != A0))
281 tmp ^= alpha_to[modnn(s[i - j] + lambda[j])];
283 omega[i] = index_of[tmp];
287 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
288 * inv(X(l))**(FCR-1) and den = lambda_pr(inv(X(l))) all in poly-form
290 for (j = count-1; j >=0; j--) {
292 for (i = deg_omega; i >= 0; i--) {
294 num1 ^= alpha_to[modnn(omega[i] + i * root[j])];
296 num2 = alpha_to[modnn(root[j] * (FCR - 1) + NN)];
299 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
300 for (i = min(deg_lambda,NROOTS-1) & ~1; i >= 0; i -=2) {
301 if(lambda[i+1] != A0)
302 den ^= alpha_to[modnn(lambda[i+1] + i * root[j])];
304 /* Apply error to data */
305 if (num1 != 0 && loc[j] >= PAD) {
306 data[loc[j]-PAD] ^= alpha_to[modnn(index_of[num1] + index_of[num2] + NN - index_of[den])];
311 if(eras_pos != NULL){
313 eras_pos[i] = loc[i];