rslib: Fix handling of of caller provided syndrome
[linux-2.6-block.git] / lib / reed_solomon / decode_rs.c
CommitLineData
dc8f923e 1// SPDX-License-Identifier: GPL-2.0
03ead842 2/*
3413e189 3 * Generic Reed Solomon encoder / decoder library
03ead842 4 *
1da177e4
LT
5 * Copyright 2002, Phil Karn, KA9Q
6 * May be used under the terms of the GNU General Public License (GPL)
7 *
8 * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
9 *
3413e189 10 * Generic data width independent code which is included by the wrappers.
1da177e4 11 */
03ead842 12{
21633981 13 struct rs_codec *rs = rsc->codec;
1da177e4
LT
14 int deg_lambda, el, deg_omega;
15 int i, j, r, k, pad;
16 int nn = rs->nn;
17 int nroots = rs->nroots;
18 int fcr = rs->fcr;
19 int prim = rs->prim;
20 int iprim = rs->iprim;
21 uint16_t *alpha_to = rs->alpha_to;
22 uint16_t *index_of = rs->index_of;
23 uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error;
1da177e4
LT
24 int count = 0;
25 uint16_t msk = (uint16_t) rs->nn;
26
45888b40
TG
27 /*
28 * The decoder buffers are in the rs control struct. They are
29 * arrays sized [nroots + 1]
30 */
31 uint16_t *lambda = rsc->buffers + RS_DECODE_LAMBDA * (nroots + 1);
32 uint16_t *syn = rsc->buffers + RS_DECODE_SYN * (nroots + 1);
33 uint16_t *b = rsc->buffers + RS_DECODE_B * (nroots + 1);
34 uint16_t *t = rsc->buffers + RS_DECODE_T * (nroots + 1);
35 uint16_t *omega = rsc->buffers + RS_DECODE_OMEGA * (nroots + 1);
36 uint16_t *root = rsc->buffers + RS_DECODE_ROOT * (nroots + 1);
37 uint16_t *reg = rsc->buffers + RS_DECODE_REG * (nroots + 1);
38 uint16_t *loc = rsc->buffers + RS_DECODE_LOC * (nroots + 1);
39
1da177e4
LT
40 /* Check length parameter for validity */
41 pad = nn - nroots - len;
a343536f 42 BUG_ON(pad < 0 || pad >= nn - nroots);
03ead842 43
1da177e4 44 /* Does the caller provide the syndrome ? */
ef4d6a85
FB
45 if (s != NULL) {
46 for (i = 0; i < nroots; i++) {
47 /* The syndrome is in index form,
48 * so nn represents zero
49 */
50 if (s[i] != nn)
51 goto decode;
52 }
53
54 /* syndrome is zero, no errors to correct */
55 return 0;
56 }
1da177e4
LT
57
58 /* form the syndromes; i.e., evaluate data(x) at roots of
59 * g(x) */
60 for (i = 0; i < nroots; i++)
61 syn[i] = (((uint16_t) data[0]) ^ invmsk) & msk;
62
63 for (j = 1; j < len; j++) {
64 for (i = 0; i < nroots; i++) {
65 if (syn[i] == 0) {
03ead842 66 syn[i] = (((uint16_t) data[j]) ^
1da177e4
LT
67 invmsk) & msk;
68 } else {
69 syn[i] = ((((uint16_t) data[j]) ^
03ead842 70 invmsk) & msk) ^
1da177e4
LT
71 alpha_to[rs_modnn(rs, index_of[syn[i]] +
72 (fcr + i) * prim)];
73 }
74 }
75 }
76
77 for (j = 0; j < nroots; j++) {
78 for (i = 0; i < nroots; i++) {
79 if (syn[i] == 0) {
80 syn[i] = ((uint16_t) par[j]) & msk;
81 } else {
03ead842 82 syn[i] = (((uint16_t) par[j]) & msk) ^
1da177e4
LT
83 alpha_to[rs_modnn(rs, index_of[syn[i]] +
84 (fcr+i)*prim)];
85 }
86 }
87 }
88 s = syn;
89
90 /* Convert syndromes to index form, checking for nonzero condition */
91 syn_error = 0;
92 for (i = 0; i < nroots; i++) {
93 syn_error |= s[i];
94 s[i] = index_of[s[i]];
95 }
96
97 if (!syn_error) {
98 /* if syndrome is zero, data[] is a codeword and there are no
99 * errors to correct. So return data[] unmodified
100 */
647cc9ec 101 return 0;
1da177e4
LT
102 }
103
104 decode:
105 memset(&lambda[1], 0, nroots * sizeof(lambda[0]));
106 lambda[0] = 1;
107
108 if (no_eras > 0) {
109 /* Init lambda to be the erasure locator polynomial */
03ead842 110 lambda[1] = alpha_to[rs_modnn(rs,
2034a42d 111 prim * (nn - 1 - (eras_pos[0] + pad)))];
1da177e4 112 for (i = 1; i < no_eras; i++) {
2034a42d 113 u = rs_modnn(rs, prim * (nn - 1 - (eras_pos[i] + pad)));
1da177e4
LT
114 for (j = i + 1; j > 0; j--) {
115 tmp = index_of[lambda[j - 1]];
116 if (tmp != nn) {
03ead842 117 lambda[j] ^=
1da177e4
LT
118 alpha_to[rs_modnn(rs, u + tmp)];
119 }
120 }
121 }
122 }
123
124 for (i = 0; i < nroots + 1; i++)
125 b[i] = index_of[lambda[i]];
126
127 /*
128 * Begin Berlekamp-Massey algorithm to determine error+erasure
129 * locator polynomial
130 */
131 r = no_eras;
132 el = no_eras;
133 while (++r <= nroots) { /* r is the step number */
134 /* Compute discrepancy at the r-th step in poly-form */
135 discr_r = 0;
136 for (i = 0; i < r; i++) {
137 if ((lambda[i] != 0) && (s[r - i - 1] != nn)) {
03ead842
TG
138 discr_r ^=
139 alpha_to[rs_modnn(rs,
1da177e4
LT
140 index_of[lambda[i]] +
141 s[r - i - 1])];
142 }
143 }
144 discr_r = index_of[discr_r]; /* Index form */
145 if (discr_r == nn) {
146 /* 2 lines below: B(x) <-- x*B(x) */
147 memmove (&b[1], b, nroots * sizeof (b[0]));
148 b[0] = nn;
149 } else {
150 /* 7 lines below: T(x) <-- lambda(x)-discr_r*x*b(x) */
151 t[0] = lambda[0];
152 for (i = 0; i < nroots; i++) {
153 if (b[i] != nn) {
03ead842 154 t[i + 1] = lambda[i + 1] ^
1da177e4
LT
155 alpha_to[rs_modnn(rs, discr_r +
156 b[i])];
157 } else
158 t[i + 1] = lambda[i + 1];
159 }
160 if (2 * el <= r + no_eras - 1) {
161 el = r + no_eras - el;
162 /*
163 * 2 lines below: B(x) <-- inv(discr_r) *
164 * lambda(x)
165 */
166 for (i = 0; i <= nroots; i++) {
167 b[i] = (lambda[i] == 0) ? nn :
168 rs_modnn(rs, index_of[lambda[i]]
169 - discr_r + nn);
170 }
171 } else {
172 /* 2 lines below: B(x) <-- x*B(x) */
173 memmove(&b[1], b, nroots * sizeof(b[0]));
174 b[0] = nn;
175 }
176 memcpy(lambda, t, (nroots + 1) * sizeof(t[0]));
177 }
178 }
179
180 /* Convert lambda to index form and compute deg(lambda(x)) */
181 deg_lambda = 0;
182 for (i = 0; i < nroots + 1; i++) {
183 lambda[i] = index_of[lambda[i]];
184 if (lambda[i] != nn)
185 deg_lambda = i;
186 }
187 /* Find roots of error+erasure locator polynomial by Chien search */
188 memcpy(&reg[1], &lambda[1], nroots * sizeof(reg[0]));
189 count = 0; /* Number of roots of lambda(x) */
190 for (i = 1, k = iprim - 1; i <= nn; i++, k = rs_modnn(rs, k + iprim)) {
191 q = 1; /* lambda[0] is always 0 */
192 for (j = deg_lambda; j > 0; j--) {
193 if (reg[j] != nn) {
194 reg[j] = rs_modnn(rs, reg[j] + j);
195 q ^= alpha_to[reg[j]];
196 }
197 }
198 if (q != 0)
199 continue; /* Not a root */
200 /* store root (index-form) and error location number */
201 root[count] = i;
202 loc[count] = k;
203 /* If we've already found max possible roots,
204 * abort the search to save time
205 */
206 if (++count == deg_lambda)
207 break;
208 }
209 if (deg_lambda != count) {
210 /*
211 * deg(lambda) unequal to number of roots => uncorrectable
212 * error detected
213 */
647cc9ec 214 return -EBADMSG;
1da177e4
LT
215 }
216 /*
217 * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
218 * x**nroots). in index form. Also find deg(omega).
219 */
220 deg_omega = deg_lambda - 1;
221 for (i = 0; i <= deg_omega; i++) {
222 tmp = 0;
223 for (j = i; j >= 0; j--) {
224 if ((s[i - j] != nn) && (lambda[j] != nn))
225 tmp ^=
226 alpha_to[rs_modnn(rs, s[i - j] + lambda[j])];
227 }
228 omega[i] = index_of[tmp];
229 }
230
231 /*
232 * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
233 * inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
234 */
235 for (j = count - 1; j >= 0; j--) {
236 num1 = 0;
237 for (i = deg_omega; i >= 0; i--) {
238 if (omega[i] != nn)
03ead842 239 num1 ^= alpha_to[rs_modnn(rs, omega[i] +
1da177e4
LT
240 i * root[j])];
241 }
242 num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)];
243 den = 0;
244
245 /* lambda[i+1] for i even is the formal derivative
246 * lambda_pr of lambda[i] */
247 for (i = min(deg_lambda, nroots - 1) & ~1; i >= 0; i -= 2) {
248 if (lambda[i + 1] != nn) {
03ead842 249 den ^= alpha_to[rs_modnn(rs, lambda[i + 1] +
1da177e4
LT
250 i * root[j])];
251 }
252 }
253 /* Apply error to data */
254 if (num1 != 0 && loc[j] >= pad) {
03ead842 255 uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] +
1da177e4
LT
256 index_of[num2] +
257 nn - index_of[den])];
258 /* Store the error correction pattern, if a
259 * correction buffer is available */
260 if (corr) {
261 corr[j] = cor;
262 } else {
263 /* If a data buffer is given and the
264 * error is inside the message,
265 * correct it */
266 if (data && (loc[j] < (nn - nroots)))
267 data[loc[j] - pad] ^= cor;
268 }
269 }
270 }
271
1da177e4
LT
272 if (eras_pos != NULL) {
273 for (i = 0; i < count; i++)
274 eras_pos[i] = loc[i] - pad;
275 }
276 return count;
277
278}