2 * The copyright in this software is being made available under the 2-clauses
3 * BSD License, included below. This software may be subject to other third
4 * party and contributor rights, including patent rights, and no such rights
5 * are granted under this license.
7 * Copyright (c) 2001-2003, David Janssens
8 * Copyright (c) 2002-2003, Yannick Verschueren
9 * Copyright (c) 2003-2005, Francois Devaux and Antonin Descampe
10 * Copyright (c) 2005, Herve Drolon, FreeImage Team
11 * Copyright (c) 2002-2005, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
12 * Copyright (c) 2005-2006, Dept. of Electronic and Information Engineering, Universita' degli Studi di Perugia, Italy
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
41 @brief Functions used to compute the Reed-Solomon parity and check of byte arrays
46 * Reed-Solomon coding and decoding
47 * Phil Karn (karn@ka9q.ampr.org) September 1996
49 * This file is derived from the program "new_rs_erasures.c" by Robert
50 * Morelos-Zaragoza (robert@spectra.eng.hawaii.edu) and Hari Thirumoorthy
51 * (harit@spectra.eng.hawaii.edu), Aug 1995
53 * I've made changes to improve performance, clean up the code and make it
54 * easier to follow. Data is now passed to the encoding and decoding functions
55 * through arguments rather than in global arrays. The decode function returns
56 * the number of corrected symbols, or -1 if the word is uncorrectable.
58 * This code supports a symbol size from 2 bits up to 16 bits,
59 * implying a block size of 3 2-bit symbols (6 bits) up to 65535
60 * 16-bit symbols (1,048,560 bits). The code parameters are set in rs.h.
62 * Note that if symbols larger than 8 bits are used, the type of each
63 * data array element switches from unsigned char to unsigned int. The
64 * caller must ensure that elements larger than the symbol range are
65 * not passed to the encoder or decoder.
72 /* This defines the type used to store an element of the Galois Field
73 * used by the code. Make sure this is something larger than a char if
74 * if anything larger than GF(256) is used.
76 * Note: unsigned char will work up to GF(256) but int seems to run
77 * faster on the Pentium.
81 /* KK = number of information symbols */
84 /* Primitive polynomials - see Lin & Costello, Appendix A,
85 * and Lee & Messerschmitt, p. 453.
87 #if(MM == 2)/* Admittedly silly */
88 int Pp[MM + 1] = { 1, 1, 1 };
92 int Pp[MM + 1] = { 1, 1, 0, 1 };
96 int Pp[MM + 1] = { 1, 1, 0, 0, 1 };
100 int Pp[MM + 1] = { 1, 0, 1, 0, 0, 1 };
104 int Pp[MM + 1] = { 1, 1, 0, 0, 0, 0, 1 };
108 int Pp[MM + 1] = { 1, 0, 0, 1, 0, 0, 0, 1 };
111 /* 1+x^2+x^3+x^4+x^8 */
112 int Pp[MM + 1] = { 1, 0, 1, 1, 1, 0, 0, 0, 1 };
116 int Pp[MM + 1] = { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
120 int Pp[MM + 1] = { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 };
124 int Pp[MM + 1] = { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
127 /* 1+x+x^4+x^6+x^12 */
128 int Pp[MM + 1] = { 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 };
131 /* 1+x+x^3+x^4+x^13 */
132 int Pp[MM + 1] = { 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
135 /* 1+x+x^6+x^10+x^14 */
136 int Pp[MM + 1] = { 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 };
140 int Pp[MM + 1] = { 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
143 /* 1+x+x^3+x^12+x^16 */
144 int Pp[MM + 1] = { 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 };
147 #error "MM must be in range 2-16"
150 /* Alpha exponent for the first root of the generator polynomial */
151 #define B0 0 /* Different from the default 1 */
153 /* index->polynomial form conversion table */
156 /* Polynomial->index form conversion table */
159 /* No legal value in index form represents zero, so
160 * we need a special value for this purpose
164 /* Generator polynomial g(x)
165 * Degree of g(x) = 2*TT
166 * has roots @**B0, @**(B0+1), ... ,@^(B0+2*TT-1)
168 /*gf Gg[NN - KK + 1];*/
171 /* Compute x % NN, where NN is 2**MM - 1,
172 * without a slow divide
179 x = (x >> MM) + (x & NN);
184 /*#define min(a,b) ((a) < (b) ? (a) : (b))*/
186 #define CLEAR(a,n) {\
188 for(ci=(n)-1;ci >=0;ci--)\
192 #define COPY(a,b,n) {\
194 for(ci=(n)-1;ci >=0;ci--)\
197 #define COPYDOWN(a,b,n) {\
199 for(ci=(n)-1;ci >=0;ci--)\
207 printf("KK must be less than 2**MM - 1\n");
215 /* generate GF(2**m) from the irreducible polynomial p(X) in p[0]..p[m]
216 lookup tables: index->polynomial form alpha_to[] contains j=alpha**i;
217 polynomial form -> index form index_of[j=alpha**i] = i
218 alpha=2 is the primitive element of GF(2**m)
219 HARI's COMMENT: (4/13/94) alpha_to[] can be used as follows:
220 Let @ represent the primitive element commonly called "alpha" that
221 is the root of the primitive polynomial p(x). Then in GF(2^m), for any
223 @^i = a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
224 where the binary vector (a(0),a(1),a(2),...,a(m-1)) is the representation
225 of the integer "alpha_to[i]" with a(0) being the LSB and a(m-1) the MSB. Thus for
226 example the polynomial representation of @^5 would be given by the binary
227 representation of the integer "alpha_to[5]".
228 Similarly, index_of[] can be used as follows:
229 As above, let @ represent the primitive element of GF(2^m) that is
230 the root of the primitive polynomial p(x). In order to find the power
231 of @ (alpha) that has the polynomial representation
232 a(0) + a(1) @ + a(2) @^2 + ... + a(m-1) @^(m-1)
233 we consider the integer "i" whose binary representation with a(0) being LSB
234 and a(m-1) MSB is (a(0),a(1),...,a(m-1)) and locate the entry
235 "index_of[i]". Now, @^index_of[i] is that element whose polynomial
236 representation is (a(0),a(1),a(2),...,a(m-1)).
238 The element alpha_to[2^m-1] = 0 always signifying that the
239 representation of "@^infinity" = 0 is (0,0,0,...,0).
240 Similarly, the element index_of[0] = A0 always signifying
241 that the power of alpha which has the polynomial representation
242 (0,0,...,0) is "infinity".
249 register int i, mask;
253 for (i = 0; i < MM; i++) {
255 Index_of[Alpha_to[i]] = i;
256 /* If Pp[i] == 1 then, term @^i occurs in poly-repr of @^MM */
258 Alpha_to[MM] ^= mask; /* Bit-wise EXOR operation */
260 mask <<= 1; /* single left-shift */
262 Index_of[Alpha_to[MM]] = MM;
264 * Have obtained poly-repr of @^MM. Poly-repr of @^(i+1) is given by
265 * poly-repr of @^i shifted left one-bit and accounting for any @^MM
266 * term that may occur when poly-repr of @^i is shifted.
269 for (i = MM + 1; i < NN; i++) {
270 if (Alpha_to[i - 1] >= mask) {
271 Alpha_to[i] = Alpha_to[MM] ^ ((Alpha_to[i - 1] ^ mask) << 1);
273 Alpha_to[i] = Alpha_to[i - 1] << 1;
275 Index_of[Alpha_to[i]] = i;
283 * Obtain the generator polynomial of the TT-error correcting, length
284 * NN=(2**MM -1) Reed Solomon code from the product of (X+@**(B0+i)), i = 0,
289 * If B0 = 1, TT = 1. deg(g(x)) = 2*TT = 2.
290 * g(x) = (x+@) (x+@**2)
292 * If B0 = 0, TT = 2. deg(g(x)) = 2*TT = 4.
293 * g(x) = (x+1) (x+@) (x+@**2) (x+@**3)
300 Gg[0] = Alpha_to[B0];
301 Gg[1] = 1; /* g(x) = (X+@**B0) initially */
302 for (i = 2; i <= NN - KK; i++) {
305 * Below multiply (Gg[0]+Gg[1]*x + ... +Gg[i]x^i) by
308 for (j = i - 1; j > 0; j--)
310 Gg[j] = Gg[j - 1] ^ Alpha_to[modnn((Index_of[Gg[j]]) + B0 + i - 1)];
314 /* Gg[0] can never be zero */
315 Gg[0] = Alpha_to[modnn((Index_of[Gg[0]]) + B0 + i - 1)];
317 /* convert Gg[] to index form for quicker encoding */
318 for (i = 0; i <= NN - KK; i++) {
319 Gg[i] = Index_of[Gg[i]];
325 * take the string of symbols in data[i], i=0..(k-1) and encode
326 * systematically to produce NN-KK parity symbols in bb[0]..bb[NN-KK-1] data[]
327 * is input and bb[] is output in polynomial form. Encoding is done by using
328 * a feedback shift register with appropriate connections specified by the
329 * elements of Gg[], which was generated above. Codeword is c(X) =
330 * data(X)*X**(NN-KK)+ b(X)
333 encode_rs(dtype *data, dtype *bb)
339 for (i = KK - 1; i >= 0; i--) {
342 return -1; /* Illegal symbol */
345 feedback = Index_of[data[i] ^ bb[NN - KK - 1]];
346 if (feedback != A0) { /* feedback term is non-zero */
347 for (j = NN - KK - 1; j > 0; j--)
349 bb[j] = bb[j - 1] ^ Alpha_to[modnn(Gg[j] + feedback)];
353 bb[0] = Alpha_to[modnn(Gg[0] + feedback)];
355 /* feedback term is zero. encoder becomes a
356 * single-byte shifter */
357 for (j = NN - KK - 1; j > 0; j--) {
367 * Performs ERRORS+ERASURES decoding of RS codes. If decoding is successful,
368 * writes the codeword into data[] itself. Otherwise data[] is unaltered.
370 * Return number of symbols corrected, or -1 if codeword is illegal
373 * First "no_eras" erasures are declared by the calling program. Then, the
374 * maximum # of errors correctable is t_after_eras = floor((NN-KK-no_eras)/2).
375 * If the number of channel errors is not greater than "t_after_eras" the
376 * transmitted codeword will be recovered. Details of algorithm can be found
377 * in R. Blahut's "Theory ... of Error-Correcting Codes".
380 eras_dec_rs(dtype *data, int *eras_pos, int no_eras)
382 int deg_lambda, el, deg_omega;
384 gf u, q, tmp, num1, num2, den, discr_r;
386 /* Err+Eras Locator poly and syndrome poly */
387 /*gf lambda[NN-KK + 1], s[NN-KK + 1];
388 gf b[NN-KK + 1], t[NN-KK + 1], omega[NN-KK + 1];
389 gf root[NN-KK], reg[NN-KK + 1], loc[NN-KK];*/
390 gf lambda[NN + 1], s[NN + 1];
391 gf b[NN + 1], t[NN + 1], omega[NN + 1];
392 gf root[NN], reg[NN + 1], loc[NN];
393 int syn_error, count;
395 /* data[] is in polynomial form, copy and convert to index form */
396 for (i = NN - 1; i >= 0; i--) {
399 return -1; /* Illegal symbol */
402 recd[i] = Index_of[data[i]];
404 /* first form the syndromes; i.e., evaluate recd(x) at roots of g(x)
405 * namely @**(B0+i), i = 0, ... ,(NN-KK-1)
408 for (i = 1; i <= NN - KK; i++) {
410 for (j = 0; j < NN; j++)
411 if (recd[j] != A0) { /* recd[j] in index form */
412 tmp ^= Alpha_to[modnn(recd[j] + (B0 + i - 1) * j)];
414 syn_error |= tmp; /* set flag if non-zero syndrome =>
416 /* store syndrome in index form */
417 s[i] = Index_of[tmp];
421 * if syndrome is zero, data[] is a codeword and there are no
422 * errors to correct. So return data[] unmodified
426 CLEAR(&lambda[1], NN - KK);
429 /* Init lambda to be the erasure locator polynomial */
430 lambda[1] = Alpha_to[eras_pos[0]];
431 for (i = 1; i < no_eras; i++) {
433 for (j = i + 1; j > 0; j--) {
434 tmp = Index_of[lambda[j - 1]];
436 lambda[j] ^= Alpha_to[modnn(u + tmp)];
441 /* find roots of the erasure location polynomial */
442 for (i = 1; i <= no_eras; i++) {
443 reg[i] = Index_of[lambda[i]];
446 for (i = 1; i <= NN; i++) {
448 for (j = 1; j <= no_eras; j++)
450 reg[j] = modnn(reg[j] + j);
451 q ^= Alpha_to[reg[j]];
454 /* store root and error location
462 if (count != no_eras) {
463 printf("\n lambda(x) is WRONG\n");
467 printf("\n Erasure positions as determined by roots of Eras Loc Poly:\n");
468 for (i = 0; i < count; i++) {
469 printf("%d ", loc[i]);
475 for (i = 0; i < NN - KK + 1; i++) {
476 b[i] = Index_of[lambda[i]];
480 * Begin Berlekamp-Massey algorithm to determine error+erasure
485 while (++r <= NN - KK) { /* r is the step number */
486 /* Compute discrepancy at the r-th step in poly-form */
488 for (i = 0; i < r; i++) {
489 if ((lambda[i] != 0) && (s[r - i] != A0)) {
490 discr_r ^= Alpha_to[modnn(Index_of[lambda[i]] + s[r - i])];
493 discr_r = Index_of[discr_r]; /* Index form */
495 /* 2 lines below: B(x) <-- x*B(x) */
496 COPYDOWN(&b[1], b, NN - KK);
499 /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
501 for (i = 0 ; i < NN - KK; i++) {
503 t[i + 1] = lambda[i + 1] ^ Alpha_to[modnn(discr_r + b[i])];
505 t[i + 1] = lambda[i + 1];
508 if (2 * el <= r + no_eras - 1) {
509 el = r + no_eras - el;
511 * 2 lines below: B(x) <-- inv(discr_r) *
514 for (i = 0; i <= NN - KK; i++) {
515 b[i] = (lambda[i] == 0) ? A0 : modnn(Index_of[lambda[i]] - discr_r + NN);
518 /* 2 lines below: B(x) <-- x*B(x) */
519 COPYDOWN(&b[1], b, NN - KK);
522 COPY(lambda, t, NN - KK + 1);
526 /* Convert lambda to index form and compute deg(lambda(x)) */
528 for (i = 0; i < NN - KK + 1; i++) {
529 lambda[i] = Index_of[lambda[i]];
530 if (lambda[i] != A0) {
535 * Find roots of the error+erasure locator polynomial. By Chien
538 COPY(®[1], &lambda[1], NN - KK);
539 count = 0; /* Number of roots of lambda(x) */
540 for (i = 1; i <= NN; i++) {
542 for (j = deg_lambda; j > 0; j--)
544 reg[j] = modnn(reg[j] + j);
545 q ^= Alpha_to[reg[j]];
548 /* store root (index-form) and error location number */
556 printf("\n Final error positions:\t");
557 for (i = 0; i < count; i++) {
558 printf("%d ", loc[i]);
562 if (deg_lambda != count) {
564 * deg(lambda) unequal to number of roots => uncorrectable
570 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
571 * x**(NN-KK)). in index form. Also find deg(omega).
574 for (i = 0; i < NN - KK; i++) {
576 j = (deg_lambda < i) ? deg_lambda : i;
577 for (; j >= 0; j--) {
578 if ((s[i + 1 - j] != A0) && (lambda[j] != A0)) {
579 tmp ^= Alpha_to[modnn(s[i + 1 - j] + lambda[j])];
585 omega[i] = Index_of[tmp];
590 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
591 * inv(X(l))**(B0-1) and den = lambda_pr(inv(X(l))) all in poly-form
593 for (j = count - 1; j >= 0; j--) {
595 for (i = deg_omega; i >= 0; i--) {
596 if (omega[i] != A0) {
597 num1 ^= Alpha_to[modnn(omega[i] + i * root[j])];
600 num2 = Alpha_to[modnn(root[j] * (B0 - 1) + NN)];
603 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
604 for (i = min(deg_lambda, NN - KK - 1) & ~1; i >= 0; i -= 2) {
605 if (lambda[i + 1] != A0) {
606 den ^= Alpha_to[modnn(lambda[i + 1] + i * root[j])];
611 printf("\n ERROR: denominator = 0\n");
615 /* Apply error to data */
617 data[loc[j]] ^= Alpha_to[modnn(Index_of[num1] + Index_of[num2] + NN -
625 #endif /* USE_JPWL */