src/rscode.c

filter: Statementstmt 66.6% 80 / 120 Branchbran 66.6% 48 / 72 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 66.6% 128 / 192 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/qrencode.c ↑ Summary src/QRCode.xs »
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 */
41 typedef unsigned char data_t;
42  
43  
44 /**
45 * Reed-Solomon codec control block
46 */
47 struct _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  
62 static RS *rslist = NULL;
63 #ifdef HAVE_LIBPTHREAD
64 static pthread_mutex_t rslist_mutex = PTHREAD_MUTEX_INITIALIZER;
65 #endif
66  
67 11128 static inline int modnn(RS *rs, int x){
68 16865 ▶ while (x >= rs->nn) {
Branch: 1 branch
BranchTrueFalseTotal
while (x >= rs->nn) { 5737 11128 16865
69 5737 x -= rs->nn;
70 5737 x = (x >> rs->mm) + (x & rs->nn);
71 }
72 11128 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 */
99 5 static 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 5 rs = NULL;
114 /* Check parameter ranges */
115 5 ▶ if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){
Branch: 2 branches
BranchTrueFalseTotal
if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){ 5 0 5
if(symsize < 0 || symsize > (int)(8*sizeof(data_t))){ 0 5 5
116 0 goto done;
117 }
118  
119 5 ▶ if(fcr < 0 || fcr >= (1<<symsize))
Branch: 2 branches
BranchTrueFalseTotal
if(fcr < 0 || fcr >= (1<<symsize)) 5 0 5
if(fcr < 0 || fcr >= (1<<symsize)) 0 5 5
120 0 goto done;
121 5 ▶ if(prim <= 0 || prim >= (1<<symsize))
Branch: 2 branches
BranchTrueFalseTotal
if(prim <= 0 || prim >= (1<<symsize)) 5 0 5
if(prim <= 0 || prim >= (1<<symsize)) 0 5 5
122 0 goto done;
123 5 ▶ if(nroots < 0 || nroots >= (1<<symsize))
Branch: 2 branches
BranchTrueFalseTotal
if(nroots < 0 || nroots >= (1<<symsize)) 5 0 5
if(nroots < 0 || nroots >= (1<<symsize)) 0 5 5
124 0 goto done; /* Can't have more roots than symbol values! */
125 5 ▶ if(pad < 0 || pad >= ((1<<symsize) -1 - nroots))
Branch: 2 branches
BranchTrueFalseTotal
if(pad < 0 || pad >= ((1<<symsize) -1 - nroots)) 5 0 5
if(pad < 0 || pad >= ((1<<symsize) -1 - nroots)) 0 5 5
126 0 goto done; /* Too much padding */
127  
128 5 rs = (RS *)calloc(1,sizeof(RS));
129 5 ▶ if(rs == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(rs == NULL) 0 5 5
130 0 goto done;
131  
132 5 rs->mm = symsize;
133 5 rs->nn = (1<<symsize)-1;
134 5 rs->pad = pad;
135  
136 5 rs->alpha_to = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
137 5 ▶ if(rs->alpha_to == NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(rs->alpha_to == NULL){ 0 5 5
138 0 free(rs);
139 0 rs = NULL;
140 0 goto done;
141 }
142 5 rs->index_of = (data_t *)malloc(sizeof(data_t)*(rs->nn+1));
143 5 ▶ if(rs->index_of == NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(rs->index_of == NULL){ 0 5 5
144 0 free(rs->alpha_to);
145 0 free(rs);
146 0 rs = NULL;
147 0 goto done;
148 }
149  
150 /* Generate Galois field lookup tables */
151 5 rs->index_of[0] = A0; /* log(zero) = -inf */
152 5 rs->alpha_to[A0] = 0; /* alpha**-inf = 0 */
153 5 sr = 1;
154 1280 ▶ for(i=0;i<rs->nn;i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<rs->nn;i++){ 1275 5 1280
155 1275 rs->index_of[sr] = i;
156 1275 rs->alpha_to[i] = sr;
157 1275 sr <<= 1;
158 1275 ▶ if(sr & (1<<symsize))
Branch: 1 branch
BranchTrueFalseTotal
if(sr & (1<<symsize)) 640 635 1275
159 640 sr ^= gfpoly;
160 1275 sr &= rs->nn;
161 }
162 5 ▶ if(sr != 1){
Branch: 1 branch
BranchTrueFalseTotal
if(sr != 1){ 0 5 5
163 /* field generator polynomial is not primitive! */
164 0 free(rs->alpha_to);
165 0 free(rs->index_of);
166 0 free(rs);
167 0 rs = NULL;
168 0 goto done;
169 }
170  
171 /* Form RS code generator polynomial from its roots */
172 5 rs->genpoly = (data_t *)malloc(sizeof(data_t)*(nroots+1));
173 5 ▶ if(rs->genpoly == NULL){
Branch: 1 branch
BranchTrueFalseTotal
if(rs->genpoly == NULL){ 0 5 5
174 0 free(rs->alpha_to);
175 0 free(rs->index_of);
176 0 free(rs);
177 0 rs = NULL;
178 0 goto done;
179 }
180 5 rs->fcr = fcr;
181 5 rs->prim = prim;
182 5 rs->nroots = nroots;
183 5 rs->gfpoly = gfpoly;
184  
185 /* Find prim-th root of 1, used in decoding */
186 5 ▶ for(iprim=1;(iprim % prim) != 0;iprim += rs->nn)
Branch: 1 branch
BranchTrueFalseTotal
for(iprim=1;(iprim % prim) != 0;iprim += rs->nn) 0 5 5
187 ;
188 5 rs->iprim = iprim / prim;
189  
190 5 rs->genpoly[0] = 1;
191 86 ▶ for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0,root=fcr*prim; i < nroots; i++,root += prim) { 81 5 86
192 81 rs->genpoly[i+1] = 1;
193  
194 /* Multiply rs->genpoly[] by @**(root + x) */
195 799 ▶ for (j = i; j > 0; j--){
Branch: 1 branch
BranchTrueFalseTotal
for (j = i; j > 0; j--){ 718 81 799
196 718 ▶ if (rs->genpoly[j] != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rs->genpoly[j] != 0) 718 0 718
197 718 rs->genpoly[j] = rs->genpoly[j-1] ^ rs->alpha_to[modnn(rs,rs->index_of[rs->genpoly[j]] + root)];
198 else
199 0 rs->genpoly[j] = rs->genpoly[j-1];
200 }
201 /* rs->genpoly[0] can never be zero */
202 81 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 91 ▶ for (i = 0; i <= nroots; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= nroots; i++) 86 5 91
206 86 rs->genpoly[i] = rs->index_of[rs->genpoly[i]];
207 5 done:;
208  
209 5 return rs;
210 }
211  
212 8 RS *init_rs(int symsize, int gfpoly, int fcr, int prim, int nroots, int pad)
213 {
214 RS *rs;
215  
216 #ifdef HAVE_LIBPTHREAD
217 8 pthread_mutex_lock(&rslist_mutex);
218 #endif
219 13 ▶ for(rs = rslist; rs != NULL; rs = rs->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(rs = rslist; rs != NULL; rs = rs->next) { 8 5 13
220 8 ▶ if(rs->pad != pad) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(rs->pad != pad) continue; 4 4 8
221 4 ▶ if(rs->nroots != nroots) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(rs->nroots != nroots) continue; 1 3 4
222 3 ▶ if(rs->mm != symsize) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(rs->mm != symsize) continue; 0 3 3
223 3 ▶ if(rs->gfpoly != gfpoly) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(rs->gfpoly != gfpoly) continue; 0 3 3
224 3 ▶ if(rs->fcr != fcr) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(rs->fcr != fcr) continue; 0 3 3
225 3 ▶ if(rs->prim != prim) continue;
Branch: 1 branch
BranchTrueFalseTotal
if(rs->prim != prim) continue; 0 3 3
226  
227 3 goto DONE;
228 }
229  
230 5 rs = init_rs_char(symsize, gfpoly, fcr, prim, nroots, pad);
231 5 ▶ if(rs == NULL) goto DONE;
Branch: 1 branch
BranchTrueFalseTotal
if(rs == NULL) goto DONE; 0 5 5
232 5 rs->next = rslist;
233 5 rslist = rs;
234  
235 8 DONE:
236 #ifdef HAVE_LIBPTHREAD
237 8 pthread_mutex_unlock(&rslist_mutex);
238 #endif
239 8 return rs;
240 }
241  
242  
243 0 void free_rs_char(RS *rs)
244 {
245 0 free(rs->alpha_to);
246 0 free(rs->index_of);
247 0 free(rs->genpoly);
248 0 free(rs);
249 0 }
250  
251 0 void free_rs_cache(void)
252 {
253 RS *rs, *next;
254  
255 #ifdef HAVE_LIBPTHREAD
256 0 pthread_mutex_lock(&rslist_mutex);
257 #endif
258 0 rs = rslist;
259 0 ▶ while(rs != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while(rs != NULL) { 0 0 0
260 0 next = rs->next;
261 0 free_rs_char(rs);
262 0 rs = next;
263 }
264 0 rslist = NULL;
265 #ifdef HAVE_LIBPTHREAD
266 0 pthread_mutex_unlock(&rslist_mutex);
267 #endif
268 0 }
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  
301 13 void encode_rs_char(RS *rs, const data_t *data, data_t *parity)
302 {
303 int i, j;
304 data_t feedback;
305  
306 13 memset(parity,0,NROOTS*sizeof(data_t));
307  
308 528 ▶ for(i=0;i<NN-NROOTS-PAD;i++){
Branch: 1 branch
BranchTrueFalseTotal
for(i=0;i<NN-NROOTS-PAD;i++){ 515 13 528
309 515 feedback = INDEX_OF[data[i] ^ parity[0]];
310 515 ▶ if(feedback != A0){ /* feedback term is non-zero */
Branch: 1 branch
BranchTrueFalseTotal
if(feedback != A0){ /* feedback term is non-zero */ 514 1 515
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 10329 ▶ for(j=1;j<NROOTS;j++)
Branch: 1 branch
BranchTrueFalseTotal
for(j=1;j<NROOTS;j++) 9815 514 10329
318 9815 parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
319 }
320 /* Shift */
321 515 memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
322 515 ▶ if(feedback != A0)
Branch: 1 branch
BranchTrueFalseTotal
if(feedback != A0) 514 1 515
323 514 parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
324 else
325 1 parity[NROOTS-1] = 0;
326 }
327 13 }
« src/qrencode.c ↑ Summary src/QRCode.xs »