1/* 2 * qrencode - QR Code encoder 3 * 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. 6 * 7 * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q 8 * (libfec is released under the GNU Lesser General Public License.) 9 * 10 * Copyright (C) 2006-2011 Kentaro Fukuchi <kentaro@fukuchi.org> 11 * 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. 16 * 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. 21 * 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 25 */ 26 27#if HAVE_CONFIG_H 28# include "config.h" 29#endif 30#include <stdlib.h> 31#include <string.h> 32#ifdef HAVE_LIBPTHREAD 33# include <pthread.h> 34#endif 35 36#include "rscode.h" 37 38/* Stuff specific to the 8-bit symbol version of the general purpose RS codecs 39 * 40 */ 41typedef unsigned char data_t; 42 43 44/** 45 * Reed-Solomon codec control block 46 */ 47struct _RS { 48 int mm; /* Bits per symbol */ 49 int nn; /* Symbols per block (= (1<<mm)-1) */ 50 data_t *alpha_to; /* log lookup table */ 51 data_t *index_of; /* Antilog lookup table */ 52 data_t *genpoly; /* Generator polynomial */ 53 int nroots; /* Number of generator roots = number of parity symbols */ 54 int fcr; /* First consecutive root, index form */ 55 int prim; /* Primitive element, index form */ 56 int iprim; /* prim-th root of 1, index form */ 57 int pad; /* Padding bytes in shortened block */ 58 int gfpoly; 59 struct _RS *next; 60}; 61 62static RS *rslist = NULL; 63#ifdef HAVE_LIBPTHREAD 64static pthread_mutex_t rslist_mutex = PTHREAD_MUTEX_INITIALIZER; 65#endif 66 67static inline int modnn(RS *rs, int x){ 68 while (x >= rs->nn) { 69 x -= rs->nn; 70 x = (x >> rs->mm) + (x & rs->nn); 71 } 72 return x; 73} 74 75 76#define MODNN(x) modnn(rs,x) 77 78#define MM (rs->mm) 79#define NN (rs->nn) 80#define ALPHA_TO (rs->alpha_to) 81#define INDEX_OF (rs->index_of) 82#define GENPOLY (rs->genpoly) 83#define NROOTS (rs->nroots) 84#define FCR (rs->fcr) 85#define PRIM (rs->prim) 86#define IPRIM (rs->iprim) 87#define PAD (rs->pad) 88#define A0 (NN) 89 90 91/* Initialize a Reed-Solomon codec 92 * symsize = symbol size, bits 93 * gfpoly = Field generator polynomial coefficients 94 * fcr = first root of RS code generator polynomial, index form 95 * prim = primitive element to generate polynomial roots 96 * nroots = RS code generator polynomial degree (number of roots) 97 * pad = padding bytes at front of shortened block 98 */ 99static RS *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad) 100{ 101 RS *rs; 102 103 104/* Common code for intializing a Reed-Solomon control block (char or int symbols) 105 * Copyright 2004 Phil Karn, KA9Q 106 * May be used under the terms of the GNU Lesser General Public License (LGPL) 107 */ 108//#undef NULL 109//#define NULL ((void *)0) 110 111 int i, j, sr,root,iprim; 112 113 rs = NULL; 114 /* Check parameter ranges */ 115 if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){ 116 goto done; 117 } 118 119 if(fcr < 0 || fcr >= (1<<symsize)) 120 goto done; 121 if(prim <= 0 || prim >= (1<<symsize)) 122 goto done; 123 if(nroots < 0 || nroots >= (1<<symsize)) 124 goto done; /* Can't have more roots than symbol values! */ 125 if(pad < 0 || pad >= ((1<<symsize) -1 - nroots)) 126 goto done; /* Too much padding */ 127 128 rs = (RS *)calloc(1,sizeof(RS)); 129 if(rs == NULL) 130 goto done; 131 132 rs->mm = symsize; 133 rs->nn = (1<<symsize)-1; 134 rs->pad = pad; 135 136 rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1)); 137 if(rs->alpha_to == NULL){ 138 free(rs); 139 rs = NULL; 140 goto done; 141 } 142 rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1)); 143 if(rs->index_of == NULL){ 144 free(rs->alpha_to); 145 free(rs); 146 rs = NULL; 147 goto done; 148 } 149 150 /* Generate Galois field lookup tables */ 151 rs->index_of[0] = A0; /* log(zero) = -inf */ 152 rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */ 153 sr = 1; 154 for(i=0;i<rs->nn;i++){ 155 rs->index_of[sr] = i; 156 rs->alpha_to[i] = sr; 157 sr <<= 1; 158 if(sr & (1<<symsize)) 159 sr ^= gfpoly; 160 sr &= rs->nn; 161 } 162 if(sr != 1){ 163 /* field generator polynomial is not primitive! */ 164 free(rs->alpha_to); 165 free(rs->index_of); 166 free(rs); 167 rs = NULL; 168 goto done; 169 } 170 171 /* Form RS code generator polynomial from its roots */ 172 rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1)); 173 if(rs->genpoly == NULL){ 174 free(rs->alpha_to); 175 free(rs->index_of); 176 free(rs); 177 rs = NULL; 178 goto done; 179 } 180 rs->fcr = fcr; 181 rs->prim = prim; 182 rs->nroots = nroots; 183 rs->gfpoly = gfpoly; 184 185 /* Find prim-th root of 1, used in decoding */ 186 for(iprim=1;(iprim % prim) != 0;iprim += rs->nn) 187 ; 188 rs->iprim = iprim / prim; 189 190 rs->genpoly[0] = 1; 191 for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) { 192 rs->genpoly[i+1] = 1; 193 194 /* Multiply rs->genpoly[] by @**(root + x) */ 195 for (j = i; j > 0; j--){ 196 if (rs->genpoly[j] != 0) 197 rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)]; 198 else 199 rs->genpoly[j] = rs->genpoly[j-1]; 200 } 201 /* rs->genpoly[0] can never be zero */ 202 rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)]; 203 } 204 /* convert rs->genpoly[] to index form for quicker encoding */ 205 for (i = 0; i <= nroots; i++) 206 rs->genpoly[i] = rs->index_of[rs->genpoly[i]]; 207 done:; 208 209 return rs; 210} 211 212RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad) 213{ 214 RS *rs; 215 216#ifdef HAVE_LIBPTHREAD 217 pthread_mutex_lock(&rslist_mutex); 218#endif 219 for(rs = rslist; rs != NULL; rs = rs->next) { 220 if(rs->pad != pad) continue; 221 if(rs->nroots != nroots) continue; 222 if(rs->mm != symsize) continue; 223 if(rs->gfpoly != gfpoly) continue; 224 if(rs->fcr != fcr) continue; 225 if(rs->prim != prim) continue; 226 227 goto DONE; 228 } 229 230 rs = init_rs_char(symsize, gfpoly, fcr, prim, nroots, pad); 231 if(rs == NULL) goto DONE; 232 rs->next = rslist; 233 rslist = rs; 234 235DONE: 236#ifdef HAVE_LIBPTHREAD 237 pthread_mutex_unlock(&rslist_mutex); 238#endif 239 return rs; 240} 241 242 243void free_rs_char(RS *rs) 244{ 245 free(rs->alpha_to); 246 free(rs->index_of); 247 free(rs->genpoly); 248 free(rs); 249} 250 251void free_rs_cache(void) 252{ 253 RS *rs, *next; 254 255#ifdef HAVE_LIBPTHREAD 256 pthread_mutex_lock(&rslist_mutex); 257#endif 258 rs = rslist; 259 while(rs != NULL) { 260 next = rs->next; 261 free_rs_char(rs); 262 rs = next; 263 } 264 rslist = NULL; 265#ifdef HAVE_LIBPTHREAD 266 pthread_mutex_unlock(&rslist_mutex); 267#endif 268} 269 270/* The guts of the Reed-Solomon encoder, meant to be #included 271 * into a function body with the following typedefs, macros and variables supplied 272 * according to the code parameters: 273 274 * data_t - a typedef for the data symbol 275 * data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded 276 * data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols 277 * NROOTS - the number of roots in the RS code generator polynomial, 278 * which is the same as the number of parity symbols in a block. 279 Integer variable or literal. 280 * 281 * NN - the total number of symbols in a RS block. Integer variable or literal. 282 * PAD - the number of pad symbols in a block. Integer variable or literal. 283 * ALPHA_TO - The address of an array of NN elements to convert Galois field 284 * elements in index (log) form to polynomial form. Read only. 285 * INDEX_OF - The address of an array of NN elements to convert Galois field 286 * elements in polynomial form to index (log) form. Read only. 287 * MODNN - a function to reduce its argument modulo NN. May be inline or a macro. 288 * GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form 289 290 * The memset() and memmove() functions are used. The appropriate header 291 * file declaring these functions (usually <string.h>) must be included by the calling 292 * program. 293 294 * Copyright 2004, Phil Karn, KA9Q 295 * May be used under the terms of the GNU Lesser General Public License (LGPL) 296 */ 297 298#undef A0 299#define A0 (NN) /* Special reserved value encoding zero in index form */ 300 301void encode_rs_char(RS *rs, const data_t *data, data_t *parity) 302{ 303 int i, j; 304 data_t feedback; 305 306 memset(parity,0,NROOTS*sizeof(data_t)); 307 308 for(i=0;i<NN-NROOTS-PAD;i++){ 309 feedback = INDEX_OF[data[i] ^ parity[0]]; 310 if(feedback != A0){ /* feedback term is non-zero */ 311#ifdef UNNORMALIZED 312 /* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must 313 * always be for the polynomials constructed by init_rs() 314 */ 315 feedback = MODNN(NN - GENPOLY[NROOTS] + feedback); 316#endif 317 for(j=1;j<NROOTS;j++) 318 parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])]; 319 } 320 /* Shift */ 321 memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1)); 322 if(feedback != A0) 323 parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])]; 324 else 325 parity[NROOTS-1] = 0; 326 } 327} 328