]> git.sur5r.net Git - glabels/blob - glabels2/qrencode-3.1.0/rscode.c
2009-09-22 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / qrencode-3.1.0 / rscode.c
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, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
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 #include <stdlib.h>
28 #include <string.h>
29 #include "rscode.h"
30
31 /* Stuff specific to the 8-bit symbol version of the general purpose RS codecs
32  *
33  */
34 typedef unsigned char data_t;
35
36
37 /**
38  * Reed-Solomon codec control block
39  */
40 struct _RS {
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 */
51         int gfpoly;
52         struct _RS *next;
53 };
54
55 static RS *rslist = NULL;
56
57 static inline int modnn(RS *rs, int x){
58         while (x >= rs->nn) {
59                 x -= rs->nn;
60                 x = (x >> rs->mm) + (x & rs->nn);
61         }
62         return x;
63 }
64
65
66 #define MODNN(x) modnn(rs,x)
67
68 #define MM (rs->mm)
69 #define NN (rs->nn)
70 #define ALPHA_TO (rs->alpha_to) 
71 #define INDEX_OF (rs->index_of)
72 #define GENPOLY (rs->genpoly)
73 #define NROOTS (rs->nroots)
74 #define FCR (rs->fcr)
75 #define PRIM (rs->prim)
76 #define IPRIM (rs->iprim)
77 #define PAD (rs->pad)
78 #define A0 (NN)
79
80
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
88  */
89 static RS *init_rs_char(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
90 {
91   RS *rs;
92
93
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)
97  */
98 //#undef NULL
99 //#define NULL ((void *)0)
100
101   int i, j, sr,root,iprim;
102
103   rs = NULL;
104   /* Check parameter ranges */
105   if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){
106     goto done;
107   }
108
109   if(fcr < 0 || fcr >= (1<<symsize))
110     goto done;
111   if(prim <= 0 || prim >= (1<<symsize))
112     goto done;
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 */
117
118   rs = (RS *)calloc(1,sizeof(RS));
119   if(rs == NULL)
120     goto done;
121
122   rs->mm = symsize;
123   rs->nn = (1<<symsize)-1;
124   rs->pad = pad;
125
126   rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
127   if(rs->alpha_to == NULL){
128     free(rs);
129     rs = NULL;
130     goto done;
131   }
132   rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
133   if(rs->index_of == NULL){
134     free(rs->alpha_to);
135     free(rs);
136     rs = NULL;
137     goto done;
138   }
139
140   /* Generate Galois field lookup tables */
141   rs->index_of[0] = A0; /* log(zero) = -inf */
142   rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
143   sr = 1;
144   for(i=0;i<rs->nn;i++){
145     rs->index_of[sr] = i;
146     rs->alpha_to[i] = sr;
147     sr <<= 1;
148     if(sr & (1<<symsize))
149       sr ^= gfpoly;
150     sr &= rs->nn;
151   }
152   if(sr != 1){
153     /* field generator polynomial is not primitive! */
154     free(rs->alpha_to);
155     free(rs->index_of);
156     free(rs);
157     rs = NULL;
158     goto done;
159   }
160
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){
164     free(rs->alpha_to);
165     free(rs->index_of);
166     free(rs);
167     rs = NULL;
168     goto done;
169   }
170   rs->fcr = fcr;
171   rs->prim = prim;
172   rs->nroots = nroots;
173   rs->gfpoly = gfpoly;
174
175   /* Find prim-th root of 1, used in decoding */
176   for(iprim=1;(iprim % prim) != 0;iprim += rs->nn)
177     ;
178   rs->iprim = iprim / prim;
179
180   rs->genpoly[0] = 1;
181   for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) {
182     rs->genpoly[i+1] = 1;
183
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)];
188       else
189         rs->genpoly[j] = rs->genpoly[j-1];
190     }
191     /* rs->genpoly[0] can never be zero */
192     rs->genpoly[0] = rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[0]] + root)];
193   }
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]];
197  done:;
198
199   return rs;
200 }
201
202 RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
203 {
204         RS *rs;
205
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;
213
214                 goto DONE;
215         }
216
217         rs = init_rs_char(symsize, gfpoly, fcr, prim, nroots, pad);
218         if(rs == NULL) goto DONE;
219         rs->next = rslist;
220         rslist = rs;
221
222 DONE:
223         return rs;
224 }
225
226
227 void free_rs_char(RS *rs)
228 {
229         free(rs->alpha_to);
230         free(rs->index_of);
231         free(rs->genpoly);
232         free(rs);
233 }
234
235 void free_rs_cache(void)
236 {
237         RS *rs, *next;
238
239         rs = rslist;
240         while(rs != NULL) {
241                 next = rs->next;
242                 free_rs_char(rs);
243                 rs = next;
244         }
245 }
246
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:
250
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.
257             * 
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
266
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
269  * program.
270
271  * Copyright 2004, Phil Karn, KA9Q
272  * May be used under the terms of the GNU Lesser General Public License (LGPL)
273  */
274
275 #undef A0
276 #define A0 (NN) /* Special reserved value encoding zero in index form */
277
278 void encode_rs_char(RS *rs, const data_t *data, data_t *parity)
279 {
280   int i, j;
281   data_t feedback;
282
283   memset(parity,0,NROOTS*sizeof(data_t));
284
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 */
288 #ifdef UNNORMALIZED
289       /* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
290        * always be for the polynomials constructed by init_rs()
291        */
292       feedback = MODNN(NN - GENPOLY[NROOTS] + feedback);
293 #endif
294       for(j=1;j<NROOTS;j++)
295         parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
296     }
297     /* Shift */
298     memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
299     if(feedback != A0)
300       parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
301     else
302       parity[NROOTS-1] = 0;
303   }
304 }