2 * qrencode - QR Code encoder
4 * Reed solomon encoder. This code is taken from Phil Karn's libfec then
5 * editted and packed into a pair of .c and .h files.
7 * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
8 * (libfec is released under the GNU Lesser General Public License.)
10 * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 /* Stuff specific to the 8-bit symbol version of the general purpose RS codecs
34 typedef unsigned char data_t;
38 * Reed-Solomon codec control block
41 int mm; /* Bits per symbol */
42 int nn; /* Symbols per block (= (1<<mm)-1) */
43 data_t *alpha_to; /* log lookup table */
44 data_t *index_of; /* Antilog lookup table */
45 data_t *genpoly; /* Generator polynomial */
46 int nroots; /* Number of generator roots = number of parity symbols */
47 int fcr; /* First consecutive root, index form */
48 int prim; /* Primitive element, index form */
49 int iprim; /* prim-th root of 1, index form */
50 int pad; /* Padding bytes in shortened block */
55 static RS *rslist = NULL;
57 static inline int modnn(RS *rs, int x){
60 x = (x >> rs->mm) + (x & rs->nn);
66 #define MODNN(x) modnn(rs,x)
70 #define ALPHA_TO (rs->alpha_to)
71 #define INDEX_OF (rs->index_of)
72 #define GENPOLY (rs->genpoly)
73 #define NROOTS (rs->nroots)
75 #define PRIM (rs->prim)
76 #define IPRIM (rs->iprim)
81 /* Initialize a Reed-Solomon codec
82 * symsize = symbol size, bits
83 * gfpoly = Field generator polynomial coefficients
84 * fcr = first root of RS code generator polynomial, index form
85 * prim = primitive element to generate polynomial roots
86 * nroots = RS code generator polynomial degree (number of roots)
87 * pad = padding bytes at front of shortened block
89 static RS *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
94 /* Common code for intializing a Reed-Solomon control block (char or int symbols)
95 * Copyright 2004 Phil Karn, KA9Q
96 * May be used under the terms of the GNU Lesser General Public License (LGPL)
99 //#define NULL ((void *)0)
101 int i, j, sr,root,iprim;
104 /* Check parameter ranges */
105 if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){
109 if(fcr < 0 || fcr >= (1<<symsize))
111 if(prim <= 0 || prim >= (1<<symsize))
113 if(nroots < 0 || nroots >= (1<<symsize))
114 goto done; /* Can't have more roots than symbol values! */
115 if(pad < 0 || pad >= ((1<<symsize) -1 - nroots))
116 goto done; /* Too much padding */
118 rs = (RS *)calloc(1,sizeof(RS));
123 rs->nn = (1<<symsize)-1;
126 rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
127 if(rs->alpha_to == NULL){
132 rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
133 if(rs->index_of == NULL){
140 /* Generate Galois field lookup tables */
141 rs->index_of[0] = A0; /* log(zero) = -inf */
142 rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
144 for(i=0;i<rs->nn;i++){
145 rs->index_of[sr] = i;
146 rs->alpha_to[i] = sr;
148 if(sr & (1<<symsize))
153 /* field generator polynomial is not primitive! */
161 /* Form RS code generator polynomial from its roots */
162 rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1));
163 if(rs->genpoly == NULL){
175 /* Find prim-th root of 1, used in decoding */
176 for(iprim=1;(iprim % prim) != 0;iprim += rs->nn)
178 rs->iprim = iprim / prim;
181 for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) {
182 rs->genpoly[i+1] = 1;
184 /* Multiply rs->genpoly[] by @**(root + x) */
185 for (j = i; j > 0; j--){
186 if (rs->genpoly[j] != 0)
187 rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)];
189 rs->genpoly[j] = rs->genpoly[j-1];
191 /* rs->genpoly[0] can never be zero */
192 rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)];
194 /* convert rs->genpoly[] to index form for quicker encoding */
195 for (i = 0; i <= nroots; i++)
196 rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
202 RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
206 for(rs = rslist; rs != NULL; rs = rs->next) {
207 if(rs->pad != pad) continue;
208 if(rs->nroots != nroots) continue;
209 if(rs->mm != symsize) continue;
210 if(rs->gfpoly != gfpoly) continue;
211 if(rs->fcr != fcr) continue;
212 if(rs->prim != prim) continue;
217 rs = init_rs_char(symsize, gfpoly, fcr, prim, nroots, pad);
218 if(rs == NULL) goto DONE;
227 void free_rs_char(RS *rs)
235 void free_rs_cache(void)
247 /* The guts of the Reed-Solomon encoder, meant to be #included
248 * into a function body with the following typedefs, macros and variables supplied
249 * according to the code parameters:
251 * data_t - a typedef for the data symbol
252 * data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded
253 * data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols
254 * NROOTS - the number of roots in the RS code generator polynomial,
255 * which is the same as the number of parity symbols in a block.
256 Integer variable or literal.
258 * NN - the total number of symbols in a RS block. Integer variable or literal.
259 * PAD - the number of pad symbols in a block. Integer variable or literal.
260 * ALPHA_TO - The address of an array of NN elements to convert Galois field
261 * elements in index (log) form to polynomial form. Read only.
262 * INDEX_OF - The address of an array of NN elements to convert Galois field
263 * elements in polynomial form to index (log) form. Read only.
264 * MODNN - a function to reduce its argument modulo NN. May be inline or a macro.
265 * GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form
267 * The memset() and memmove() functions are used. The appropriate header
268 * file declaring these functions (usually <string.h>) must be included by the calling
271 * Copyright 2004, Phil Karn, KA9Q
272 * May be used under the terms of the GNU Lesser General Public License (LGPL)
276 #define A0 (NN) /* Special reserved value encoding zero in index form */
278 void encode_rs_char(RS *rs, const data_t *data, data_t *parity)
283 memset(parity,0,NROOTS*sizeof(data_t));
285 for(i=0;i<NN-NROOTS-PAD;i++){
286 feedback = INDEX_OF[data[i] ^ parity[0]];
287 if(feedback != A0){ /* feedback term is non-zero */
289 /* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
290 * always be for the polynomials constructed by init_rs()
292 feedback = MODNN(NN - GENPOLY[NROOTS] + feedback);
294 for(j=1;j<NROOTS;j++)
295 parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
298 memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
300 parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
302 parity[NROOTS-1] = 0;