Commit 1dd7fdb163645f453f5ae55686511b6fcc2314cd

Authored by Jörn Engel
Committed by David Woodhouse
1 parent c32b8dcc45

[RSLIB] BUG() when passing illegal parameters to decode_rs8() or decode_rs16()

Returning -ERANGE should never happen.

Signed-off-by: Jörn Engel <joern@logfs.org>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>

Showing 1 changed file with 1 additions and 2 deletions Inline Diff

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