2 * Reed-Solomon encoder, 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 /* Initialize a Reed-Solomon codec
29 * symsize = symbol size, bits
30 * gfpoly = Field generator polynomial coefficients
31 * fcr = first root of RS code generator polynomial, index form
32 * prim = primitive element to generate polynomial roots
33 * nroots = RS code generator polynomial degree (number of roots)
34 * pad = padding bytes at front of shortened block
36 struct rs *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
39 int i, j, sr, root, iprim;
41 /* Check parameter ranges */
42 if (symsize < 0 || symsize > 8 * (int)sizeof(data_t))
44 if (fcr < 0 || fcr >= (1<<symsize))
46 if (prim <= 0 || prim >= (1<<symsize))
48 if (nroots < 0 || nroots >= (1<<symsize))
49 return NULL; /* Can't have more roots than symbol values! */
51 if (pad < 0 || pad >= ((1<<symsize) - 1 - nroots))
52 return NULL; /* Too much padding */
54 rs = calloc(1, sizeof(struct rs));
59 rs->nn = (1<<symsize) - 1;
62 rs->alpha_to = malloc(sizeof(data_t) * (rs->nn + 1));
63 if (rs->alpha_to == NULL) {
67 rs->index_of = malloc(sizeof(data_t) * (rs->nn + 1));
68 if (rs->index_of == NULL) {
73 memset(rs->index_of, 0, sizeof(data_t) * (rs->nn + 1));
75 /* Generate Galois field lookup tables */
76 rs->index_of[0] = A0; /* log(zero) = -inf */
77 rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
79 for (i = 0; i < rs->nn; i++) {
88 /* field generator polynomial is not primitive! */
95 /* Form RS code generator polynomial from its roots */
96 rs->genpoly = malloc(sizeof(data_t) * (nroots + 1));
97 if (rs->genpoly == NULL) {
108 /* Find prim-th root of 1, used in decoding */
109 for (iprim = 1; (iprim % prim) != 0; iprim += rs->nn)
111 rs->iprim = iprim / prim;
114 for (i = 0, root = fcr * prim; i < nroots; i++, root += prim) {
115 rs->genpoly[i + 1] = 1;
117 /* Multiply rs->genpoly[] by @**(root + x) */
118 for (j = i; j > 0; j--){
119 if (rs->genpoly[j] != 0)
120 rs->genpoly[j] = rs->genpoly[j - 1] ^ rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[j]] + root)];
122 rs->genpoly[j] = rs->genpoly[j - 1];
124 /* rs->genpoly[0] can never be zero */
125 rs->genpoly[0] = rs->alpha_to[modnn(rs, rs->index_of[rs->genpoly[0]] + root)];
127 /* convert rs->genpoly[] to index form for quicker encoding */
128 for (i = 0; i <= nroots; i++)
129 rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
134 void free_rs_char(struct rs *rs)
145 void encode_rs_char(struct rs *rs, data_t *data, data_t *parity)
150 memset(parity, 0, rs->nroots * sizeof(data_t));
152 for (i = 0; i < rs->nn - rs->nroots - rs->pad; i++) {
153 feedback = rs->index_of[data[i] ^ parity[0]];
154 if (feedback != A0) {
155 /* feedback term is non-zero */
157 /* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
158 * always be for the polynomials constructed by init_rs() */
159 feedback = modnn(rs, rs->nn - rs->genpoly[rs->nroots] + feedback);
161 for (j = 1; j < rs->nroots; j++)
162 parity[j] ^= rs->alpha_to[modnn(rs, feedback + rs->genpoly[rs->nroots - j])];
166 memmove(&parity[0], &parity[1], sizeof(data_t) * (rs->nroots - 1));
169 parity[rs->nroots - 1] = rs->alpha_to[modnn(rs, feedback + rs->genpoly[0])];
171 parity[rs->nroots - 1] = 0;