rscode.c 8.5 KB

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