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