rsecc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * qrencode - QR Code encoder
  3. *
  4. * Reed solomon error correction code encoder specialized for QR code.
  5. * This code is rewritten by Kentaro Fukuchi, referring to the FEC library
  6. * developed by Phil Karn (KA9Q).
  7. *
  8. * Copyright (C) 2002, 2003, 2004, 2006 Phil Karn, KA9Q
  9. * Copyright (C) 2014-2017 Kentaro Fukuchi <kentaro@fukuchi.org>
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #if HAVE_CONFIG_H
  26. # include "config.h"
  27. #endif
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #if HAVE_LIBPTHREAD
  31. #include <pthread.h>
  32. #endif
  33. #include "rsecc.h"
  34. #if HAVE_LIBPTHREAD
  35. static pthread_mutex_t RSECC_mutex = PTHREAD_MUTEX_INITIALIZER;
  36. #endif
  37. static int initialized = 0;
  38. #define SYMBOL_SIZE (8)
  39. #define symbols ((1U << SYMBOL_SIZE) - 1)
  40. static const unsigned int proot = 0x11d; /* stands for x^8+x^4+x^3+x^2+1 (see pp.37 of JIS X0510:2004) */
  41. /* min/max codeword length of ECC, calculated from the specification. */
  42. #define min_length (2)
  43. #define max_length (30)
  44. #define max_generatorSize (max_length)
  45. static unsigned char alpha[symbols + 1];
  46. static unsigned char aindex[symbols + 1];
  47. static unsigned char generator[max_length - min_length + 1][max_generatorSize + 1];
  48. static unsigned char generatorInitialized[max_length - min_length + 1];
  49. static void RSECC_initLookupTable(void)
  50. {
  51. unsigned int i, b;
  52. alpha[symbols] = 0;
  53. aindex[0] = symbols;
  54. b = 1;
  55. for(i = 0; i < symbols; i++) {
  56. alpha[i] = b;
  57. aindex[b] = i;
  58. b <<= 1;
  59. if(b & (symbols + 1)) {
  60. b ^= proot;
  61. }
  62. b &= symbols;
  63. }
  64. }
  65. static void RSECC_init(void)
  66. {
  67. RSECC_initLookupTable();
  68. memset(generatorInitialized, 0, (max_length - min_length + 1));
  69. initialized = 1;
  70. }
  71. static void generator_init(size_t length)
  72. {
  73. size_t i, j;
  74. int g[max_generatorSize + 1];
  75. g[0] = 1;
  76. for(i = 0; i < length; i++) {
  77. g[i + 1] = 1;
  78. /* Because g[0] never be zero, skipped some conditional checks. */
  79. for(j = i; j > 0; j--) {
  80. g[j] = g[j - 1] ^ alpha[(aindex[g[j]] + i) % symbols];
  81. }
  82. g[0] = alpha[(aindex[g[0]] + i) % symbols];
  83. }
  84. for(i = 0; i <= length; i++) {
  85. generator[length - min_length][i] = aindex[g[i]];
  86. }
  87. generatorInitialized[length - min_length] = 1;
  88. }
  89. int RSECC_encode(size_t data_length, size_t ecc_length, const unsigned char *data, unsigned char *ecc)
  90. {
  91. size_t i, j;
  92. unsigned char feedback;
  93. unsigned char *gen;
  94. #if HAVE_LIBPTHREAD
  95. pthread_mutex_lock(&RSECC_mutex);
  96. #endif
  97. if(!initialized) {
  98. RSECC_init();
  99. }
  100. #if HAVE_LIBPTHREAD
  101. pthread_mutex_unlock(&RSECC_mutex);
  102. #endif
  103. if(ecc_length > max_length) return -1;
  104. memset(ecc, 0, ecc_length);
  105. #if HAVE_LIBPTHREAD
  106. pthread_mutex_lock(&RSECC_mutex);
  107. #endif
  108. if(!generatorInitialized[ecc_length - min_length]) generator_init(ecc_length);
  109. #if HAVE_LIBPTHREAD
  110. pthread_mutex_unlock(&RSECC_mutex);
  111. #endif
  112. gen = generator[ecc_length - min_length];
  113. for(i = 0; i < data_length; i++) {
  114. feedback = aindex[data[i] ^ ecc[0]];
  115. if(feedback != symbols) {
  116. for(j = 1; j < ecc_length; j++) {
  117. ecc[j] ^= alpha[(unsigned int)(feedback + gen[ecc_length - j]) % symbols];
  118. }
  119. }
  120. memmove(&ecc[0], &ecc[1], ecc_length - 1);
  121. if(feedback != symbols) {
  122. ecc[ecc_length - 1] = alpha[(unsigned int)(feedback + gen[0]) % symbols];
  123. } else {
  124. ecc[ecc_length - 1] = 0;
  125. }
  126. }
  127. return 0;
  128. }