2 * Reed-Solomon decoder, based on libfec
4 * Copyright (C) 2002, Phil Karn, KA9Q
5 * libcryptsetup modifications
6 * Copyright (C) 2017-2020 Red Hat, Inc. All rights reserved.
8 * This file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This file is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this file; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 int decode_rs_char(struct rs* rs, data_t* data)
30 int deg_lambda, el, deg_omega, syn_error, count;
32 data_t q, tmp, num1, num2, den, discr_r;
33 /* FIXME: remove VLAs here */
34 data_t lambda[rs->nroots + 1], s[rs->nroots]; /* Err+Eras Locator poly and syndrome poly */
35 data_t b[rs->nroots + 1], t[rs->nroots + 1], omega[rs->nroots + 1];
36 data_t root[rs->nroots], reg[rs->nroots + 1], loc[rs->nroots];
38 memset(s, 0, rs->nroots * sizeof(data_t));
39 memset(b, 0, (rs->nroots + 1) * sizeof(data_t));
41 /* form the syndromes; i.e., evaluate data(x) at roots of g(x) */
42 for (i = 0; i < rs->nroots; i++)
45 for (j = 1; j < rs->nn - rs->pad; j++) {
46 for (i = 0; i < rs->nroots; i++) {
50 s[i] = data[j] ^ rs->alpha_to[modnn(rs, rs->index_of[s[i]] + (rs->fcr + i) * rs->prim)];
55 /* Convert syndromes to index form, checking for nonzero condition */
57 for (i = 0; i < rs->nroots; i++) {
59 s[i] = rs->index_of[s[i]];
63 * if syndrome is zero, data[] is a codeword and there are no
64 * errors to correct. So return data[] unmodified
69 memset(&lambda[1], 0, rs->nroots * sizeof(lambda[0]));
72 for (i = 0; i < rs->nroots + 1; i++)
73 b[i] = rs->index_of[lambda[i]];
76 * Begin Berlekamp-Massey algorithm to determine error+erasure
81 while (++r <= rs->nroots) { /* r is the step number */
82 /* Compute discrepancy at the r-th step in poly-form */
84 for (i = 0; i < r; i++) {
85 if ((lambda[i] != 0) && (s[r - i - 1] != A0)) {
86 discr_r ^= rs->alpha_to[modnn(rs, rs->index_of[lambda[i]] + s[r - i - 1])];
89 discr_r = rs->index_of[discr_r]; /* Index form */
91 /* 2 lines below: B(x) <-- x*B(x) */
92 memmove(&b[1], b, rs->nroots * sizeof(b[0]));
95 /* 7 lines below: T(x) <-- lambda(x) - discr_r*x*b(x) */
97 for (i = 0; i < rs->nroots; i++) {
99 t[i + 1] = lambda[i + 1] ^ rs->alpha_to[modnn(rs, discr_r + b[i])];
101 t[i + 1] = lambda[i + 1];
103 if (2 * el <= r - 1) {
106 * 2 lines below: B(x) <-- inv(discr_r) *
109 for (i = 0; i <= rs->nroots; i++)
110 b[i] = (lambda[i] == 0) ? A0 : modnn(rs, rs->index_of[lambda[i]] - discr_r + rs->nn);
112 /* 2 lines below: B(x) <-- x*B(x) */
113 memmove(&b[1], b, rs->nroots * sizeof(b[0]));
116 memcpy(lambda, t, (rs->nroots + 1) * sizeof(t[0]));
120 /* Convert lambda to index form and compute deg(lambda(x)) */
122 for (i = 0; i < rs->nroots + 1; i++) {
123 lambda[i] = rs->index_of[lambda[i]];
127 /* Find roots of the error+erasure locator polynomial by Chien search */
128 memcpy(®[1], &lambda[1], rs->nroots * sizeof(reg[0]));
129 count = 0; /* Number of roots of lambda(x) */
130 for (i = 1, k = rs->iprim - 1; i <= rs->nn; i++, k = modnn(rs, k + rs->iprim)) {
131 q = 1; /* lambda[0] is always 0 */
132 for (j = deg_lambda; j > 0; j--) {
134 reg[j] = modnn(rs, reg[j] + j);
135 q ^= rs->alpha_to[reg[j]];
139 continue; /* Not a root */
141 /* store root (index-form) and error location number */
144 /* If we've already found max possible roots, abort the search to save time */
145 if (++count == deg_lambda)
150 * deg(lambda) unequal to number of roots => uncorrectable
153 if (deg_lambda != count)
157 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
158 * x**rs->nroots). in index form. Also find deg(omega).
160 deg_omega = deg_lambda - 1;
161 for (i = 0; i <= deg_omega; i++) {
163 for (j = i; j >= 0; j--) {
164 if ((s[i - j] != A0) && (lambda[j] != A0))
165 tmp ^= rs->alpha_to[modnn(rs, s[i - j] + lambda[j])];
167 omega[i] = rs->index_of[tmp];
171 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
172 * inv(X(l))**(rs->fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
174 for (j = count - 1; j >= 0; j--) {
176 for (i = deg_omega; i >= 0; i--) {
178 num1 ^= rs->alpha_to[modnn(rs, omega[i] + i * root[j])];
180 num2 = rs->alpha_to[modnn(rs, root[j] * (rs->fcr - 1) + rs->nn)];
183 /* lambda[i+1] for i even is the formal derivative lambda_pr of lambda[i] */
184 for (i = RS_MIN(deg_lambda, rs->nroots - 1) & ~1; i >= 0; i -= 2) {
185 if (lambda[i + 1] != A0)
186 den ^= rs->alpha_to[modnn(rs, lambda[i + 1] + i * root[j])];
189 /* Apply error to data */
190 if (num1 != 0 && loc[j] >= rs->pad) {
191 data[loc[j] - rs->pad] ^= rs->alpha_to[modnn(rs, rs->index_of[num1] +
192 rs->index_of[num2] + rs->nn - rs->index_of[den])];