cheetah_qrcode 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/ext/cheetah_qrcode/cheetah_qrcode.c +176 -0
- data/ext/cheetah_qrcode/extconf.rb +13 -0
- data/ext/cheetah_qrcode/qrcodegen.c +1027 -0
- data/ext/cheetah_qrcode/qrcodegen.h +385 -0
- data/ext/cheetah_qrcode/spng.c +6980 -0
- data/ext/cheetah_qrcode/spng.h +537 -0
- data/lib/cheetah_qrcode/version.rb +5 -0
- data/lib/cheetah_qrcode.rb +11 -0
- metadata +80 -0
@@ -0,0 +1,1027 @@
|
|
1
|
+
/*
|
2
|
+
* QR Code generator library (C)
|
3
|
+
*
|
4
|
+
* Copyright (c) Project Nayuki. (MIT License)
|
5
|
+
* https://www.nayuki.io/page/qr-code-generator-library
|
6
|
+
*
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
8
|
+
* this software and associated documentation files (the "Software"), to deal in
|
9
|
+
* the Software without restriction, including without limitation the rights to
|
10
|
+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
11
|
+
* the Software, and to permit persons to whom the Software is furnished to do so,
|
12
|
+
* subject to the following conditions:
|
13
|
+
* - The above copyright notice and this permission notice shall be included in
|
14
|
+
* all copies or substantial portions of the Software.
|
15
|
+
* - The Software is provided "as is", without warranty of any kind, express or
|
16
|
+
* implied, including but not limited to the warranties of merchantability,
|
17
|
+
* fitness for a particular purpose and noninfringement. In no event shall the
|
18
|
+
* authors or copyright holders be liable for any claim, damages or other
|
19
|
+
* liability, whether in an action of contract, tort or otherwise, arising from,
|
20
|
+
* out of or in connection with the Software or the use or other dealings in the
|
21
|
+
* Software.
|
22
|
+
*/
|
23
|
+
|
24
|
+
#include <assert.h>
|
25
|
+
#include <limits.h>
|
26
|
+
#include <stdlib.h>
|
27
|
+
#include <string.h>
|
28
|
+
#include "qrcodegen.h"
|
29
|
+
|
30
|
+
#ifndef QRCODEGEN_TEST
|
31
|
+
#define testable static // Keep functions private
|
32
|
+
#else
|
33
|
+
#define testable // Expose private functions
|
34
|
+
#endif
|
35
|
+
|
36
|
+
|
37
|
+
/*---- Forward declarations for private functions ----*/
|
38
|
+
|
39
|
+
// Regarding all public and private functions defined in this source file:
|
40
|
+
// - They require all pointer/array arguments to be not null unless the array length is zero.
|
41
|
+
// - They only read input scalar/array arguments, write to output pointer/array
|
42
|
+
// arguments, and return scalar values; they are "pure" functions.
|
43
|
+
// - They don't read mutable global variables or write to any global variables.
|
44
|
+
// - They don't perform I/O, read the clock, print to console, etc.
|
45
|
+
// - They allocate a small and constant amount of stack memory.
|
46
|
+
// - They don't allocate or free any memory on the heap.
|
47
|
+
// - They don't recurse or mutually recurse. All the code
|
48
|
+
// could be inlined into the top-level public functions.
|
49
|
+
// - They run in at most quadratic time with respect to input arguments.
|
50
|
+
// Most functions run in linear time, and some in constant time.
|
51
|
+
// There are no unbounded loops or non-obvious termination conditions.
|
52
|
+
// - They are completely thread-safe if the caller does not give the
|
53
|
+
// same writable buffer to concurrent calls to these functions.
|
54
|
+
|
55
|
+
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen);
|
56
|
+
|
57
|
+
testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]);
|
58
|
+
testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl);
|
59
|
+
testable int getNumRawDataModules(int ver);
|
60
|
+
|
61
|
+
testable void reedSolomonComputeDivisor(int degree, uint8_t result[]);
|
62
|
+
testable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen,
|
63
|
+
const uint8_t generator[], int degree, uint8_t result[]);
|
64
|
+
testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y);
|
65
|
+
|
66
|
+
testable void initializeFunctionModules(int version, uint8_t qrcode[]);
|
67
|
+
static void drawLightFunctionModules(uint8_t qrcode[], int version);
|
68
|
+
static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]);
|
69
|
+
testable int getAlignmentPatternPositions(int version, uint8_t result[7]);
|
70
|
+
static void fillRectangle(int left, int top, int width, int height, uint8_t qrcode[]);
|
71
|
+
|
72
|
+
static void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[]);
|
73
|
+
static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qrcodegen_Mask mask);
|
74
|
+
static long getPenaltyScore(const uint8_t qrcode[]);
|
75
|
+
static int finderPenaltyCountPatterns(const int runHistory[7], int qrsize);
|
76
|
+
static int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, int runHistory[7], int qrsize);
|
77
|
+
static void finderPenaltyAddHistory(int currentRunLength, int runHistory[7], int qrsize);
|
78
|
+
|
79
|
+
testable bool getModuleBounded(const uint8_t qrcode[], int x, int y);
|
80
|
+
testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isDark);
|
81
|
+
testable void setModuleUnbounded(uint8_t qrcode[], int x, int y, bool isDark);
|
82
|
+
static bool getBit(int x, int i);
|
83
|
+
|
84
|
+
testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars);
|
85
|
+
testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version);
|
86
|
+
static int numCharCountBits(enum qrcodegen_Mode mode, int version);
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
/*---- Private tables of constants ----*/
|
91
|
+
|
92
|
+
// The set of all legal characters in alphanumeric mode, where each character
|
93
|
+
// value maps to the index in the string. For checking text and encoding segments.
|
94
|
+
static const char *ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
95
|
+
|
96
|
+
// Sentinel value for use in only some functions.
|
97
|
+
#define LENGTH_OVERFLOW -1
|
98
|
+
|
99
|
+
// For generating error correction codes.
|
100
|
+
testable const int8_t ECC_CODEWORDS_PER_BLOCK[4][41] = {
|
101
|
+
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
102
|
+
//0, 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 Error correction level
|
103
|
+
{-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low
|
104
|
+
{-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium
|
105
|
+
{-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile
|
106
|
+
{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High
|
107
|
+
};
|
108
|
+
|
109
|
+
#define qrcodegen_REED_SOLOMON_DEGREE_MAX 30 // Based on the table above
|
110
|
+
|
111
|
+
// For generating error correction codes.
|
112
|
+
testable const int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
|
113
|
+
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
114
|
+
//0, 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 Error correction level
|
115
|
+
{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low
|
116
|
+
{-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium
|
117
|
+
{-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile
|
118
|
+
{-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High
|
119
|
+
};
|
120
|
+
|
121
|
+
// For automatic mask pattern selection.
|
122
|
+
static const int PENALTY_N1 = 3;
|
123
|
+
static const int PENALTY_N2 = 3;
|
124
|
+
static const int PENALTY_N3 = 40;
|
125
|
+
static const int PENALTY_N4 = 10;
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
/*---- High-level QR Code encoding functions ----*/
|
130
|
+
|
131
|
+
// Public function - see documentation comment in header file.
|
132
|
+
bool qrcodegen_encodeText(const char *text, uint8_t tempBuffer[], uint8_t qrcode[],
|
133
|
+
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
|
134
|
+
|
135
|
+
size_t textLen = strlen(text);
|
136
|
+
if (textLen == 0)
|
137
|
+
return qrcodegen_encodeSegmentsAdvanced(NULL, 0, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode);
|
138
|
+
size_t bufLen = (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(maxVersion);
|
139
|
+
|
140
|
+
struct qrcodegen_Segment seg;
|
141
|
+
if (qrcodegen_isNumeric(text)) {
|
142
|
+
if (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_NUMERIC, textLen) > bufLen)
|
143
|
+
goto fail;
|
144
|
+
seg = qrcodegen_makeNumeric(text, tempBuffer);
|
145
|
+
} else if (qrcodegen_isAlphanumeric(text)) {
|
146
|
+
if (qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_ALPHANUMERIC, textLen) > bufLen)
|
147
|
+
goto fail;
|
148
|
+
seg = qrcodegen_makeAlphanumeric(text, tempBuffer);
|
149
|
+
} else {
|
150
|
+
if (textLen > bufLen)
|
151
|
+
goto fail;
|
152
|
+
for (size_t i = 0; i < textLen; i++)
|
153
|
+
tempBuffer[i] = (uint8_t)text[i];
|
154
|
+
seg.mode = qrcodegen_Mode_BYTE;
|
155
|
+
seg.bitLength = calcSegmentBitLength(seg.mode, textLen);
|
156
|
+
if (seg.bitLength == LENGTH_OVERFLOW)
|
157
|
+
goto fail;
|
158
|
+
seg.numChars = (int)textLen;
|
159
|
+
seg.data = tempBuffer;
|
160
|
+
}
|
161
|
+
return qrcodegen_encodeSegmentsAdvanced(&seg, 1, ecl, minVersion, maxVersion, mask, boostEcl, tempBuffer, qrcode);
|
162
|
+
|
163
|
+
fail:
|
164
|
+
qrcode[0] = 0; // Set size to invalid value for safety
|
165
|
+
return false;
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
// Public function - see documentation comment in header file.
|
170
|
+
bool qrcodegen_encodeBinary(uint8_t dataAndTemp[], size_t dataLen, uint8_t qrcode[],
|
171
|
+
enum qrcodegen_Ecc ecl, int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl) {
|
172
|
+
|
173
|
+
struct qrcodegen_Segment seg;
|
174
|
+
seg.mode = qrcodegen_Mode_BYTE;
|
175
|
+
seg.bitLength = calcSegmentBitLength(seg.mode, dataLen);
|
176
|
+
if (seg.bitLength == LENGTH_OVERFLOW) {
|
177
|
+
qrcode[0] = 0; // Set size to invalid value for safety
|
178
|
+
return false;
|
179
|
+
}
|
180
|
+
seg.numChars = (int)dataLen;
|
181
|
+
seg.data = dataAndTemp;
|
182
|
+
return qrcodegen_encodeSegmentsAdvanced(&seg, 1, ecl, minVersion, maxVersion, mask, boostEcl, dataAndTemp, qrcode);
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
// Appends the given number of low-order bits of the given value to the given byte-based
|
187
|
+
// bit buffer, increasing the bit length. Requires 0 <= numBits <= 16 and val < 2^numBits.
|
188
|
+
testable void appendBitsToBuffer(unsigned int val, int numBits, uint8_t buffer[], int *bitLen) {
|
189
|
+
assert(0 <= numBits && numBits <= 16 && (unsigned long)val >> numBits == 0);
|
190
|
+
for (int i = numBits - 1; i >= 0; i--, (*bitLen)++)
|
191
|
+
buffer[*bitLen >> 3] |= ((val >> i) & 1) << (7 - (*bitLen & 7));
|
192
|
+
}
|
193
|
+
|
194
|
+
|
195
|
+
|
196
|
+
/*---- Low-level QR Code encoding functions ----*/
|
197
|
+
|
198
|
+
// Public function - see documentation comment in header file.
|
199
|
+
bool qrcodegen_encodeSegments(const struct qrcodegen_Segment segs[], size_t len,
|
200
|
+
enum qrcodegen_Ecc ecl, uint8_t tempBuffer[], uint8_t qrcode[]) {
|
201
|
+
return qrcodegen_encodeSegmentsAdvanced(segs, len, ecl,
|
202
|
+
qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, qrcodegen_Mask_AUTO, true, tempBuffer, qrcode);
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
// Public function - see documentation comment in header file.
|
207
|
+
bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], size_t len, enum qrcodegen_Ecc ecl,
|
208
|
+
int minVersion, int maxVersion, enum qrcodegen_Mask mask, bool boostEcl, uint8_t tempBuffer[], uint8_t qrcode[]) {
|
209
|
+
assert(segs != NULL || len == 0);
|
210
|
+
assert(qrcodegen_VERSION_MIN <= minVersion && minVersion <= maxVersion && maxVersion <= qrcodegen_VERSION_MAX);
|
211
|
+
assert(0 <= (int)ecl && (int)ecl <= 3 && -1 <= (int)mask && (int)mask <= 7);
|
212
|
+
|
213
|
+
// Find the minimal version number to use
|
214
|
+
int version, dataUsedBits;
|
215
|
+
for (version = minVersion; ; version++) {
|
216
|
+
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
|
217
|
+
dataUsedBits = getTotalBits(segs, len, version);
|
218
|
+
if (dataUsedBits != LENGTH_OVERFLOW && dataUsedBits <= dataCapacityBits)
|
219
|
+
break; // This version number is found to be suitable
|
220
|
+
if (version >= maxVersion) { // All versions in the range could not fit the given data
|
221
|
+
qrcode[0] = 0; // Set size to invalid value for safety
|
222
|
+
return false;
|
223
|
+
}
|
224
|
+
}
|
225
|
+
assert(dataUsedBits != LENGTH_OVERFLOW);
|
226
|
+
|
227
|
+
// Increase the error correction level while the data still fits in the current version number
|
228
|
+
for (int i = (int)qrcodegen_Ecc_MEDIUM; i <= (int)qrcodegen_Ecc_HIGH; i++) { // From low to high
|
229
|
+
if (boostEcl && dataUsedBits <= getNumDataCodewords(version, (enum qrcodegen_Ecc)i) * 8)
|
230
|
+
ecl = (enum qrcodegen_Ecc)i;
|
231
|
+
}
|
232
|
+
|
233
|
+
// Concatenate all segments to create the data bit string
|
234
|
+
memset(qrcode, 0, (size_t)qrcodegen_BUFFER_LEN_FOR_VERSION(version) * sizeof(qrcode[0]));
|
235
|
+
int bitLen = 0;
|
236
|
+
for (size_t i = 0; i < len; i++) {
|
237
|
+
const struct qrcodegen_Segment *seg = &segs[i];
|
238
|
+
appendBitsToBuffer((unsigned int)seg->mode, 4, qrcode, &bitLen);
|
239
|
+
appendBitsToBuffer((unsigned int)seg->numChars, numCharCountBits(seg->mode, version), qrcode, &bitLen);
|
240
|
+
for (int j = 0; j < seg->bitLength; j++) {
|
241
|
+
int bit = (seg->data[j >> 3] >> (7 - (j & 7))) & 1;
|
242
|
+
appendBitsToBuffer((unsigned int)bit, 1, qrcode, &bitLen);
|
243
|
+
}
|
244
|
+
}
|
245
|
+
assert(bitLen == dataUsedBits);
|
246
|
+
|
247
|
+
// Add terminator and pad up to a byte if applicable
|
248
|
+
int dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
|
249
|
+
assert(bitLen <= dataCapacityBits);
|
250
|
+
int terminatorBits = dataCapacityBits - bitLen;
|
251
|
+
if (terminatorBits > 4)
|
252
|
+
terminatorBits = 4;
|
253
|
+
appendBitsToBuffer(0, terminatorBits, qrcode, &bitLen);
|
254
|
+
appendBitsToBuffer(0, (8 - bitLen % 8) % 8, qrcode, &bitLen);
|
255
|
+
assert(bitLen % 8 == 0);
|
256
|
+
|
257
|
+
// Pad with alternating bytes until data capacity is reached
|
258
|
+
for (uint8_t padByte = 0xEC; bitLen < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
|
259
|
+
appendBitsToBuffer(padByte, 8, qrcode, &bitLen);
|
260
|
+
|
261
|
+
// Compute ECC, draw modules
|
262
|
+
addEccAndInterleave(qrcode, version, ecl, tempBuffer);
|
263
|
+
initializeFunctionModules(version, qrcode);
|
264
|
+
drawCodewords(tempBuffer, getNumRawDataModules(version) / 8, qrcode);
|
265
|
+
drawLightFunctionModules(qrcode, version);
|
266
|
+
initializeFunctionModules(version, tempBuffer);
|
267
|
+
|
268
|
+
// Do masking
|
269
|
+
if (mask == qrcodegen_Mask_AUTO) { // Automatically choose best mask
|
270
|
+
long minPenalty = LONG_MAX;
|
271
|
+
for (int i = 0; i < 8; i++) {
|
272
|
+
enum qrcodegen_Mask msk = (enum qrcodegen_Mask)i;
|
273
|
+
applyMask(tempBuffer, qrcode, msk);
|
274
|
+
drawFormatBits(ecl, msk, qrcode);
|
275
|
+
long penalty = getPenaltyScore(qrcode);
|
276
|
+
if (penalty < minPenalty) {
|
277
|
+
mask = msk;
|
278
|
+
minPenalty = penalty;
|
279
|
+
}
|
280
|
+
applyMask(tempBuffer, qrcode, msk); // Undoes the mask due to XOR
|
281
|
+
}
|
282
|
+
}
|
283
|
+
assert(0 <= (int)mask && (int)mask <= 7);
|
284
|
+
applyMask(tempBuffer, qrcode, mask); // Apply the final choice of mask
|
285
|
+
drawFormatBits(ecl, mask, qrcode); // Overwrite old format bits
|
286
|
+
return true;
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
/*---- Error correction code generation functions ----*/
|
292
|
+
|
293
|
+
// Appends error correction bytes to each block of the given data array, then interleaves
|
294
|
+
// bytes from the blocks and stores them in the result array. data[0 : dataLen] contains
|
295
|
+
// the input data. data[dataLen : rawCodewords] is used as a temporary work area and will
|
296
|
+
// be clobbered by this function. The final answer is stored in result[0 : rawCodewords].
|
297
|
+
testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ecc ecl, uint8_t result[]) {
|
298
|
+
// Calculate parameter numbers
|
299
|
+
assert(0 <= (int)ecl && (int)ecl < 4 && qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
|
300
|
+
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[(int)ecl][version];
|
301
|
+
int blockEccLen = ECC_CODEWORDS_PER_BLOCK [(int)ecl][version];
|
302
|
+
int rawCodewords = getNumRawDataModules(version) / 8;
|
303
|
+
int dataLen = getNumDataCodewords(version, ecl);
|
304
|
+
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
305
|
+
int shortBlockDataLen = rawCodewords / numBlocks - blockEccLen;
|
306
|
+
|
307
|
+
// Split data into blocks, calculate ECC, and interleave
|
308
|
+
// (not concatenate) the bytes into a single sequence
|
309
|
+
uint8_t rsdiv[qrcodegen_REED_SOLOMON_DEGREE_MAX];
|
310
|
+
reedSolomonComputeDivisor(blockEccLen, rsdiv);
|
311
|
+
const uint8_t *dat = data;
|
312
|
+
for (int i = 0; i < numBlocks; i++) {
|
313
|
+
int datLen = shortBlockDataLen + (i < numShortBlocks ? 0 : 1);
|
314
|
+
uint8_t *ecc = &data[dataLen]; // Temporary storage
|
315
|
+
reedSolomonComputeRemainder(dat, datLen, rsdiv, blockEccLen, ecc);
|
316
|
+
for (int j = 0, k = i; j < datLen; j++, k += numBlocks) { // Copy data
|
317
|
+
if (j == shortBlockDataLen)
|
318
|
+
k -= numShortBlocks;
|
319
|
+
result[k] = dat[j];
|
320
|
+
}
|
321
|
+
for (int j = 0, k = dataLen + i; j < blockEccLen; j++, k += numBlocks) // Copy ECC
|
322
|
+
result[k] = ecc[j];
|
323
|
+
dat += datLen;
|
324
|
+
}
|
325
|
+
}
|
326
|
+
|
327
|
+
|
328
|
+
// Returns the number of 8-bit codewords that can be used for storing data (not ECC),
|
329
|
+
// for the given version number and error correction level. The result is in the range [9, 2956].
|
330
|
+
testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) {
|
331
|
+
int v = version, e = (int)ecl;
|
332
|
+
assert(0 <= e && e < 4);
|
333
|
+
return getNumRawDataModules(v) / 8
|
334
|
+
- ECC_CODEWORDS_PER_BLOCK [e][v]
|
335
|
+
* NUM_ERROR_CORRECTION_BLOCKS[e][v];
|
336
|
+
}
|
337
|
+
|
338
|
+
|
339
|
+
// Returns the number of data bits that can be stored in a QR Code of the given version number, after
|
340
|
+
// all function modules are excluded. This includes remainder bits, so it might not be a multiple of 8.
|
341
|
+
// The result is in the range [208, 29648]. This could be implemented as a 40-entry lookup table.
|
342
|
+
testable int getNumRawDataModules(int ver) {
|
343
|
+
assert(qrcodegen_VERSION_MIN <= ver && ver <= qrcodegen_VERSION_MAX);
|
344
|
+
int result = (16 * ver + 128) * ver + 64;
|
345
|
+
if (ver >= 2) {
|
346
|
+
int numAlign = ver / 7 + 2;
|
347
|
+
result -= (25 * numAlign - 10) * numAlign - 55;
|
348
|
+
if (ver >= 7)
|
349
|
+
result -= 36;
|
350
|
+
}
|
351
|
+
assert(208 <= result && result <= 29648);
|
352
|
+
return result;
|
353
|
+
}
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
/*---- Reed-Solomon ECC generator functions ----*/
|
358
|
+
|
359
|
+
// Computes a Reed-Solomon ECC generator polynomial for the given degree, storing in result[0 : degree].
|
360
|
+
// This could be implemented as a lookup table over all possible parameter values, instead of as an algorithm.
|
361
|
+
testable void reedSolomonComputeDivisor(int degree, uint8_t result[]) {
|
362
|
+
assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);
|
363
|
+
// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
|
364
|
+
// For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array {255, 8, 93}.
|
365
|
+
memset(result, 0, (size_t)degree * sizeof(result[0]));
|
366
|
+
result[degree - 1] = 1; // Start off with the monomial x^0
|
367
|
+
|
368
|
+
// Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
369
|
+
// drop the highest monomial term which is always 1x^degree.
|
370
|
+
// Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
371
|
+
uint8_t root = 1;
|
372
|
+
for (int i = 0; i < degree; i++) {
|
373
|
+
// Multiply the current product by (x - r^i)
|
374
|
+
for (int j = 0; j < degree; j++) {
|
375
|
+
result[j] = reedSolomonMultiply(result[j], root);
|
376
|
+
if (j + 1 < degree)
|
377
|
+
result[j] ^= result[j + 1];
|
378
|
+
}
|
379
|
+
root = reedSolomonMultiply(root, 0x02);
|
380
|
+
}
|
381
|
+
}
|
382
|
+
|
383
|
+
|
384
|
+
// Computes the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
385
|
+
// The remainder when data[0 : dataLen] is divided by divisor[0 : degree] is stored in result[0 : degree].
|
386
|
+
// All polynomials are in big endian, and the generator has an implicit leading 1 term.
|
387
|
+
testable void reedSolomonComputeRemainder(const uint8_t data[], int dataLen,
|
388
|
+
const uint8_t generator[], int degree, uint8_t result[]) {
|
389
|
+
assert(1 <= degree && degree <= qrcodegen_REED_SOLOMON_DEGREE_MAX);
|
390
|
+
memset(result, 0, (size_t)degree * sizeof(result[0]));
|
391
|
+
for (int i = 0; i < dataLen; i++) { // Polynomial division
|
392
|
+
uint8_t factor = data[i] ^ result[0];
|
393
|
+
memmove(&result[0], &result[1], (size_t)(degree - 1) * sizeof(result[0]));
|
394
|
+
result[degree - 1] = 0;
|
395
|
+
for (int j = 0; j < degree; j++)
|
396
|
+
result[j] ^= reedSolomonMultiply(generator[j], factor);
|
397
|
+
}
|
398
|
+
}
|
399
|
+
|
400
|
+
#undef qrcodegen_REED_SOLOMON_DEGREE_MAX
|
401
|
+
|
402
|
+
|
403
|
+
// Returns the product of the two given field elements modulo GF(2^8/0x11D).
|
404
|
+
// All inputs are valid. This could be implemented as a 256*256 lookup table.
|
405
|
+
testable uint8_t reedSolomonMultiply(uint8_t x, uint8_t y) {
|
406
|
+
// Russian peasant multiplication
|
407
|
+
uint8_t z = 0;
|
408
|
+
for (int i = 7; i >= 0; i--) {
|
409
|
+
z = (uint8_t)((z << 1) ^ ((z >> 7) * 0x11D));
|
410
|
+
z ^= ((y >> i) & 1) * x;
|
411
|
+
}
|
412
|
+
return z;
|
413
|
+
}
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
/*---- Drawing function modules ----*/
|
418
|
+
|
419
|
+
// Clears the given QR Code grid with light modules for the given
|
420
|
+
// version's size, then marks every function module as dark.
|
421
|
+
testable void initializeFunctionModules(int version, uint8_t qrcode[]) {
|
422
|
+
// Initialize QR Code
|
423
|
+
int qrsize = version * 4 + 17;
|
424
|
+
memset(qrcode, 0, (size_t)((qrsize * qrsize + 7) / 8 + 1) * sizeof(qrcode[0]));
|
425
|
+
qrcode[0] = (uint8_t)qrsize;
|
426
|
+
|
427
|
+
// Fill horizontal and vertical timing patterns
|
428
|
+
fillRectangle(6, 0, 1, qrsize, qrcode);
|
429
|
+
fillRectangle(0, 6, qrsize, 1, qrcode);
|
430
|
+
|
431
|
+
// Fill 3 finder patterns (all corners except bottom right) and format bits
|
432
|
+
fillRectangle(0, 0, 9, 9, qrcode);
|
433
|
+
fillRectangle(qrsize - 8, 0, 8, 9, qrcode);
|
434
|
+
fillRectangle(0, qrsize - 8, 9, 8, qrcode);
|
435
|
+
|
436
|
+
// Fill numerous alignment patterns
|
437
|
+
uint8_t alignPatPos[7];
|
438
|
+
int numAlign = getAlignmentPatternPositions(version, alignPatPos);
|
439
|
+
for (int i = 0; i < numAlign; i++) {
|
440
|
+
for (int j = 0; j < numAlign; j++) {
|
441
|
+
// Don't draw on the three finder corners
|
442
|
+
if (!((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0)))
|
443
|
+
fillRectangle(alignPatPos[i] - 2, alignPatPos[j] - 2, 5, 5, qrcode);
|
444
|
+
}
|
445
|
+
}
|
446
|
+
|
447
|
+
// Fill version blocks
|
448
|
+
if (version >= 7) {
|
449
|
+
fillRectangle(qrsize - 11, 0, 3, 6, qrcode);
|
450
|
+
fillRectangle(0, qrsize - 11, 6, 3, qrcode);
|
451
|
+
}
|
452
|
+
}
|
453
|
+
|
454
|
+
|
455
|
+
// Draws light function modules and possibly some dark modules onto the given QR Code, without changing
|
456
|
+
// non-function modules. This does not draw the format bits. This requires all function modules to be previously
|
457
|
+
// marked dark (namely by initializeFunctionModules()), because this may skip redrawing dark function modules.
|
458
|
+
static void drawLightFunctionModules(uint8_t qrcode[], int version) {
|
459
|
+
// Draw horizontal and vertical timing patterns
|
460
|
+
int qrsize = qrcodegen_getSize(qrcode);
|
461
|
+
for (int i = 7; i < qrsize - 7; i += 2) {
|
462
|
+
setModuleBounded(qrcode, 6, i, false);
|
463
|
+
setModuleBounded(qrcode, i, 6, false);
|
464
|
+
}
|
465
|
+
|
466
|
+
// Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
|
467
|
+
for (int dy = -4; dy <= 4; dy++) {
|
468
|
+
for (int dx = -4; dx <= 4; dx++) {
|
469
|
+
int dist = abs(dx);
|
470
|
+
if (abs(dy) > dist)
|
471
|
+
dist = abs(dy);
|
472
|
+
if (dist == 2 || dist == 4) {
|
473
|
+
setModuleUnbounded(qrcode, 3 + dx, 3 + dy, false);
|
474
|
+
setModuleUnbounded(qrcode, qrsize - 4 + dx, 3 + dy, false);
|
475
|
+
setModuleUnbounded(qrcode, 3 + dx, qrsize - 4 + dy, false);
|
476
|
+
}
|
477
|
+
}
|
478
|
+
}
|
479
|
+
|
480
|
+
// Draw numerous alignment patterns
|
481
|
+
uint8_t alignPatPos[7];
|
482
|
+
int numAlign = getAlignmentPatternPositions(version, alignPatPos);
|
483
|
+
for (int i = 0; i < numAlign; i++) {
|
484
|
+
for (int j = 0; j < numAlign; j++) {
|
485
|
+
if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))
|
486
|
+
continue; // Don't draw on the three finder corners
|
487
|
+
for (int dy = -1; dy <= 1; dy++) {
|
488
|
+
for (int dx = -1; dx <= 1; dx++)
|
489
|
+
setModuleBounded(qrcode, alignPatPos[i] + dx, alignPatPos[j] + dy, dx == 0 && dy == 0);
|
490
|
+
}
|
491
|
+
}
|
492
|
+
}
|
493
|
+
|
494
|
+
// Draw version blocks
|
495
|
+
if (version >= 7) {
|
496
|
+
// Calculate error correction code and pack bits
|
497
|
+
int rem = version; // version is uint6, in the range [7, 40]
|
498
|
+
for (int i = 0; i < 12; i++)
|
499
|
+
rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
|
500
|
+
long bits = (long)version << 12 | rem; // uint18
|
501
|
+
assert(bits >> 18 == 0);
|
502
|
+
|
503
|
+
// Draw two copies
|
504
|
+
for (int i = 0; i < 6; i++) {
|
505
|
+
for (int j = 0; j < 3; j++) {
|
506
|
+
int k = qrsize - 11 + j;
|
507
|
+
setModuleBounded(qrcode, k, i, (bits & 1) != 0);
|
508
|
+
setModuleBounded(qrcode, i, k, (bits & 1) != 0);
|
509
|
+
bits >>= 1;
|
510
|
+
}
|
511
|
+
}
|
512
|
+
}
|
513
|
+
}
|
514
|
+
|
515
|
+
|
516
|
+
// Draws two copies of the format bits (with its own error correction code) based
|
517
|
+
// on the given mask and error correction level. This always draws all modules of
|
518
|
+
// the format bits, unlike drawLightFunctionModules() which might skip dark modules.
|
519
|
+
static void drawFormatBits(enum qrcodegen_Ecc ecl, enum qrcodegen_Mask mask, uint8_t qrcode[]) {
|
520
|
+
// Calculate error correction code and pack bits
|
521
|
+
assert(0 <= (int)mask && (int)mask <= 7);
|
522
|
+
static const int table[] = {1, 0, 3, 2};
|
523
|
+
int data = table[(int)ecl] << 3 | (int)mask; // errCorrLvl is uint2, mask is uint3
|
524
|
+
int rem = data;
|
525
|
+
for (int i = 0; i < 10; i++)
|
526
|
+
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
|
527
|
+
int bits = (data << 10 | rem) ^ 0x5412; // uint15
|
528
|
+
assert(bits >> 15 == 0);
|
529
|
+
|
530
|
+
// Draw first copy
|
531
|
+
for (int i = 0; i <= 5; i++)
|
532
|
+
setModuleBounded(qrcode, 8, i, getBit(bits, i));
|
533
|
+
setModuleBounded(qrcode, 8, 7, getBit(bits, 6));
|
534
|
+
setModuleBounded(qrcode, 8, 8, getBit(bits, 7));
|
535
|
+
setModuleBounded(qrcode, 7, 8, getBit(bits, 8));
|
536
|
+
for (int i = 9; i < 15; i++)
|
537
|
+
setModuleBounded(qrcode, 14 - i, 8, getBit(bits, i));
|
538
|
+
|
539
|
+
// Draw second copy
|
540
|
+
int qrsize = qrcodegen_getSize(qrcode);
|
541
|
+
for (int i = 0; i < 8; i++)
|
542
|
+
setModuleBounded(qrcode, qrsize - 1 - i, 8, getBit(bits, i));
|
543
|
+
for (int i = 8; i < 15; i++)
|
544
|
+
setModuleBounded(qrcode, 8, qrsize - 15 + i, getBit(bits, i));
|
545
|
+
setModuleBounded(qrcode, 8, qrsize - 8, true); // Always dark
|
546
|
+
}
|
547
|
+
|
548
|
+
|
549
|
+
// Calculates and stores an ascending list of positions of alignment patterns
|
550
|
+
// for this version number, returning the length of the list (in the range [0,7]).
|
551
|
+
// Each position is in the range [0,177), and are used on both the x and y axes.
|
552
|
+
// This could be implemented as lookup table of 40 variable-length lists of unsigned bytes.
|
553
|
+
testable int getAlignmentPatternPositions(int version, uint8_t result[7]) {
|
554
|
+
if (version == 1)
|
555
|
+
return 0;
|
556
|
+
int numAlign = version / 7 + 2;
|
557
|
+
int step = (version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2;
|
558
|
+
for (int i = numAlign - 1, pos = version * 4 + 10; i >= 1; i--, pos -= step)
|
559
|
+
result[i] = (uint8_t)pos;
|
560
|
+
result[0] = 6;
|
561
|
+
return numAlign;
|
562
|
+
}
|
563
|
+
|
564
|
+
|
565
|
+
// Sets every module in the range [left : left + width] * [top : top + height] to dark.
|
566
|
+
static void fillRectangle(int left, int top, int width, int height, uint8_t qrcode[]) {
|
567
|
+
for (int dy = 0; dy < height; dy++) {
|
568
|
+
for (int dx = 0; dx < width; dx++)
|
569
|
+
setModuleBounded(qrcode, left + dx, top + dy, true);
|
570
|
+
}
|
571
|
+
}
|
572
|
+
|
573
|
+
|
574
|
+
|
575
|
+
/*---- Drawing data modules and masking ----*/
|
576
|
+
|
577
|
+
// Draws the raw codewords (including data and ECC) onto the given QR Code. This requires the initial state of
|
578
|
+
// the QR Code to be dark at function modules and light at codeword modules (including unused remainder bits).
|
579
|
+
static void drawCodewords(const uint8_t data[], int dataLen, uint8_t qrcode[]) {
|
580
|
+
int qrsize = qrcodegen_getSize(qrcode);
|
581
|
+
int i = 0; // Bit index into the data
|
582
|
+
// Do the funny zigzag scan
|
583
|
+
for (int right = qrsize - 1; right >= 1; right -= 2) { // Index of right column in each column pair
|
584
|
+
if (right == 6)
|
585
|
+
right = 5;
|
586
|
+
for (int vert = 0; vert < qrsize; vert++) { // Vertical counter
|
587
|
+
for (int j = 0; j < 2; j++) {
|
588
|
+
int x = right - j; // Actual x coordinate
|
589
|
+
bool upward = ((right + 1) & 2) == 0;
|
590
|
+
int y = upward ? qrsize - 1 - vert : vert; // Actual y coordinate
|
591
|
+
if (!getModuleBounded(qrcode, x, y) && i < dataLen * 8) {
|
592
|
+
bool dark = getBit(data[i >> 3], 7 - (i & 7));
|
593
|
+
setModuleBounded(qrcode, x, y, dark);
|
594
|
+
i++;
|
595
|
+
}
|
596
|
+
// If this QR Code has any remainder bits (0 to 7), they were assigned as
|
597
|
+
// 0/false/light by the constructor and are left unchanged by this method
|
598
|
+
}
|
599
|
+
}
|
600
|
+
}
|
601
|
+
assert(i == dataLen * 8);
|
602
|
+
}
|
603
|
+
|
604
|
+
|
605
|
+
// XORs the codeword modules in this QR Code with the given mask pattern
|
606
|
+
// and given pattern of function modules. The codeword bits must be drawn
|
607
|
+
// before masking. Due to the arithmetic of XOR, calling applyMask() with
|
608
|
+
// the same mask value a second time will undo the mask. A final well-formed
|
609
|
+
// QR Code needs exactly one (not zero, two, etc.) mask applied.
|
610
|
+
static void applyMask(const uint8_t functionModules[], uint8_t qrcode[], enum qrcodegen_Mask mask) {
|
611
|
+
assert(0 <= (int)mask && (int)mask <= 7); // Disallows qrcodegen_Mask_AUTO
|
612
|
+
int qrsize = qrcodegen_getSize(qrcode);
|
613
|
+
for (int y = 0; y < qrsize; y++) {
|
614
|
+
for (int x = 0; x < qrsize; x++) {
|
615
|
+
if (getModuleBounded(functionModules, x, y))
|
616
|
+
continue;
|
617
|
+
bool invert;
|
618
|
+
switch ((int)mask) {
|
619
|
+
case 0: invert = (x + y) % 2 == 0; break;
|
620
|
+
case 1: invert = y % 2 == 0; break;
|
621
|
+
case 2: invert = x % 3 == 0; break;
|
622
|
+
case 3: invert = (x + y) % 3 == 0; break;
|
623
|
+
case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
|
624
|
+
case 5: invert = x * y % 2 + x * y % 3 == 0; break;
|
625
|
+
case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
|
626
|
+
case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
|
627
|
+
default: assert(false); return;
|
628
|
+
}
|
629
|
+
bool val = getModuleBounded(qrcode, x, y);
|
630
|
+
setModuleBounded(qrcode, x, y, val ^ invert);
|
631
|
+
}
|
632
|
+
}
|
633
|
+
}
|
634
|
+
|
635
|
+
|
636
|
+
// Calculates and returns the penalty score based on state of the given QR Code's current modules.
|
637
|
+
// This is used by the automatic mask choice algorithm to find the mask pattern that yields the lowest score.
|
638
|
+
static long getPenaltyScore(const uint8_t qrcode[]) {
|
639
|
+
int qrsize = qrcodegen_getSize(qrcode);
|
640
|
+
long result = 0;
|
641
|
+
|
642
|
+
// Adjacent modules in row having same color, and finder-like patterns
|
643
|
+
for (int y = 0; y < qrsize; y++) {
|
644
|
+
bool runColor = false;
|
645
|
+
int runX = 0;
|
646
|
+
int runHistory[7] = {0};
|
647
|
+
for (int x = 0; x < qrsize; x++) {
|
648
|
+
if (getModuleBounded(qrcode, x, y) == runColor) {
|
649
|
+
runX++;
|
650
|
+
if (runX == 5)
|
651
|
+
result += PENALTY_N1;
|
652
|
+
else if (runX > 5)
|
653
|
+
result++;
|
654
|
+
} else {
|
655
|
+
finderPenaltyAddHistory(runX, runHistory, qrsize);
|
656
|
+
if (!runColor)
|
657
|
+
result += finderPenaltyCountPatterns(runHistory, qrsize) * PENALTY_N3;
|
658
|
+
runColor = getModuleBounded(qrcode, x, y);
|
659
|
+
runX = 1;
|
660
|
+
}
|
661
|
+
}
|
662
|
+
result += finderPenaltyTerminateAndCount(runColor, runX, runHistory, qrsize) * PENALTY_N3;
|
663
|
+
}
|
664
|
+
// Adjacent modules in column having same color, and finder-like patterns
|
665
|
+
for (int x = 0; x < qrsize; x++) {
|
666
|
+
bool runColor = false;
|
667
|
+
int runY = 0;
|
668
|
+
int runHistory[7] = {0};
|
669
|
+
for (int y = 0; y < qrsize; y++) {
|
670
|
+
if (getModuleBounded(qrcode, x, y) == runColor) {
|
671
|
+
runY++;
|
672
|
+
if (runY == 5)
|
673
|
+
result += PENALTY_N1;
|
674
|
+
else if (runY > 5)
|
675
|
+
result++;
|
676
|
+
} else {
|
677
|
+
finderPenaltyAddHistory(runY, runHistory, qrsize);
|
678
|
+
if (!runColor)
|
679
|
+
result += finderPenaltyCountPatterns(runHistory, qrsize) * PENALTY_N3;
|
680
|
+
runColor = getModuleBounded(qrcode, x, y);
|
681
|
+
runY = 1;
|
682
|
+
}
|
683
|
+
}
|
684
|
+
result += finderPenaltyTerminateAndCount(runColor, runY, runHistory, qrsize) * PENALTY_N3;
|
685
|
+
}
|
686
|
+
|
687
|
+
// 2*2 blocks of modules having same color
|
688
|
+
for (int y = 0; y < qrsize - 1; y++) {
|
689
|
+
for (int x = 0; x < qrsize - 1; x++) {
|
690
|
+
bool color = getModuleBounded(qrcode, x, y);
|
691
|
+
if ( color == getModuleBounded(qrcode, x + 1, y) &&
|
692
|
+
color == getModuleBounded(qrcode, x, y + 1) &&
|
693
|
+
color == getModuleBounded(qrcode, x + 1, y + 1))
|
694
|
+
result += PENALTY_N2;
|
695
|
+
}
|
696
|
+
}
|
697
|
+
|
698
|
+
// Balance of dark and light modules
|
699
|
+
int dark = 0;
|
700
|
+
for (int y = 0; y < qrsize; y++) {
|
701
|
+
for (int x = 0; x < qrsize; x++) {
|
702
|
+
if (getModuleBounded(qrcode, x, y))
|
703
|
+
dark++;
|
704
|
+
}
|
705
|
+
}
|
706
|
+
int total = qrsize * qrsize; // Note that size is odd, so dark/total != 1/2
|
707
|
+
// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%
|
708
|
+
int k = (int)((labs(dark * 20L - total * 10L) + total - 1) / total) - 1;
|
709
|
+
assert(0 <= k && k <= 9);
|
710
|
+
result += k * PENALTY_N4;
|
711
|
+
assert(0 <= result && result <= 2568888L); // Non-tight upper bound based on default values of PENALTY_N1, ..., N4
|
712
|
+
return result;
|
713
|
+
}
|
714
|
+
|
715
|
+
|
716
|
+
// Can only be called immediately after a light run is added, and
|
717
|
+
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
718
|
+
static int finderPenaltyCountPatterns(const int runHistory[7], int qrsize) {
|
719
|
+
int n = runHistory[1];
|
720
|
+
assert(n <= qrsize * 3); (void)qrsize;
|
721
|
+
bool core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
|
722
|
+
// The maximum QR Code size is 177, hence the dark run length n <= 177.
|
723
|
+
// Arithmetic is promoted to int, so n*4 will not overflow.
|
724
|
+
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)
|
725
|
+
+ (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
|
726
|
+
}
|
727
|
+
|
728
|
+
|
729
|
+
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
730
|
+
static int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, int runHistory[7], int qrsize) {
|
731
|
+
if (currentRunColor) { // Terminate dark run
|
732
|
+
finderPenaltyAddHistory(currentRunLength, runHistory, qrsize);
|
733
|
+
currentRunLength = 0;
|
734
|
+
}
|
735
|
+
currentRunLength += qrsize; // Add light border to final run
|
736
|
+
finderPenaltyAddHistory(currentRunLength, runHistory, qrsize);
|
737
|
+
return finderPenaltyCountPatterns(runHistory, qrsize);
|
738
|
+
}
|
739
|
+
|
740
|
+
|
741
|
+
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
742
|
+
static void finderPenaltyAddHistory(int currentRunLength, int runHistory[7], int qrsize) {
|
743
|
+
if (runHistory[0] == 0)
|
744
|
+
currentRunLength += qrsize; // Add light border to initial run
|
745
|
+
memmove(&runHistory[1], &runHistory[0], 6 * sizeof(runHistory[0]));
|
746
|
+
runHistory[0] = currentRunLength;
|
747
|
+
}
|
748
|
+
|
749
|
+
|
750
|
+
|
751
|
+
/*---- Basic QR Code information ----*/
|
752
|
+
|
753
|
+
// Public function - see documentation comment in header file.
|
754
|
+
int qrcodegen_getSize(const uint8_t qrcode[]) {
|
755
|
+
assert(qrcode != NULL);
|
756
|
+
int result = qrcode[0];
|
757
|
+
assert((qrcodegen_VERSION_MIN * 4 + 17) <= result
|
758
|
+
&& result <= (qrcodegen_VERSION_MAX * 4 + 17));
|
759
|
+
return result;
|
760
|
+
}
|
761
|
+
|
762
|
+
|
763
|
+
// Public function - see documentation comment in header file.
|
764
|
+
bool qrcodegen_getModule(const uint8_t qrcode[], int x, int y) {
|
765
|
+
assert(qrcode != NULL);
|
766
|
+
int qrsize = qrcode[0];
|
767
|
+
return (0 <= x && x < qrsize && 0 <= y && y < qrsize) && getModuleBounded(qrcode, x, y);
|
768
|
+
}
|
769
|
+
|
770
|
+
|
771
|
+
// Returns the color of the module at the given coordinates, which must be in bounds.
|
772
|
+
testable bool getModuleBounded(const uint8_t qrcode[], int x, int y) {
|
773
|
+
int qrsize = qrcode[0];
|
774
|
+
assert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize);
|
775
|
+
int index = y * qrsize + x;
|
776
|
+
return getBit(qrcode[(index >> 3) + 1], index & 7);
|
777
|
+
}
|
778
|
+
|
779
|
+
|
780
|
+
// Sets the color of the module at the given coordinates, which must be in bounds.
|
781
|
+
testable void setModuleBounded(uint8_t qrcode[], int x, int y, bool isDark) {
|
782
|
+
int qrsize = qrcode[0];
|
783
|
+
assert(21 <= qrsize && qrsize <= 177 && 0 <= x && x < qrsize && 0 <= y && y < qrsize);
|
784
|
+
int index = y * qrsize + x;
|
785
|
+
int bitIndex = index & 7;
|
786
|
+
int byteIndex = (index >> 3) + 1;
|
787
|
+
if (isDark)
|
788
|
+
qrcode[byteIndex] |= 1 << bitIndex;
|
789
|
+
else
|
790
|
+
qrcode[byteIndex] &= (1 << bitIndex) ^ 0xFF;
|
791
|
+
}
|
792
|
+
|
793
|
+
|
794
|
+
// Sets the color of the module at the given coordinates, doing nothing if out of bounds.
|
795
|
+
testable void setModuleUnbounded(uint8_t qrcode[], int x, int y, bool isDark) {
|
796
|
+
int qrsize = qrcode[0];
|
797
|
+
if (0 <= x && x < qrsize && 0 <= y && y < qrsize)
|
798
|
+
setModuleBounded(qrcode, x, y, isDark);
|
799
|
+
}
|
800
|
+
|
801
|
+
|
802
|
+
// Returns true iff the i'th bit of x is set to 1. Requires x >= 0 and 0 <= i <= 14.
|
803
|
+
static bool getBit(int x, int i) {
|
804
|
+
return ((x >> i) & 1) != 0;
|
805
|
+
}
|
806
|
+
|
807
|
+
|
808
|
+
|
809
|
+
/*---- Segment handling ----*/
|
810
|
+
|
811
|
+
// Public function - see documentation comment in header file.
|
812
|
+
bool qrcodegen_isNumeric(const char *text) {
|
813
|
+
assert(text != NULL);
|
814
|
+
for (; *text != '\0'; text++) {
|
815
|
+
if (*text < '0' || *text > '9')
|
816
|
+
return false;
|
817
|
+
}
|
818
|
+
return true;
|
819
|
+
}
|
820
|
+
|
821
|
+
|
822
|
+
// Public function - see documentation comment in header file.
|
823
|
+
bool qrcodegen_isAlphanumeric(const char *text) {
|
824
|
+
assert(text != NULL);
|
825
|
+
for (; *text != '\0'; text++) {
|
826
|
+
if (strchr(ALPHANUMERIC_CHARSET, *text) == NULL)
|
827
|
+
return false;
|
828
|
+
}
|
829
|
+
return true;
|
830
|
+
}
|
831
|
+
|
832
|
+
|
833
|
+
// Public function - see documentation comment in header file.
|
834
|
+
size_t qrcodegen_calcSegmentBufferSize(enum qrcodegen_Mode mode, size_t numChars) {
|
835
|
+
int temp = calcSegmentBitLength(mode, numChars);
|
836
|
+
if (temp == LENGTH_OVERFLOW)
|
837
|
+
return SIZE_MAX;
|
838
|
+
assert(0 <= temp && temp <= INT16_MAX);
|
839
|
+
return ((size_t)temp + 7) / 8;
|
840
|
+
}
|
841
|
+
|
842
|
+
|
843
|
+
// Returns the number of data bits needed to represent a segment
|
844
|
+
// containing the given number of characters using the given mode. Notes:
|
845
|
+
// - Returns LENGTH_OVERFLOW on failure, i.e. numChars > INT16_MAX
|
846
|
+
// or the number of needed bits exceeds INT16_MAX (i.e. 32767).
|
847
|
+
// - Otherwise, all valid results are in the range [0, INT16_MAX].
|
848
|
+
// - For byte mode, numChars measures the number of bytes, not Unicode code points.
|
849
|
+
// - For ECI mode, numChars must be 0, and the worst-case number of bits is returned.
|
850
|
+
// An actual ECI segment can have shorter data. For non-ECI modes, the result is exact.
|
851
|
+
testable int calcSegmentBitLength(enum qrcodegen_Mode mode, size_t numChars) {
|
852
|
+
// All calculations are designed to avoid overflow on all platforms
|
853
|
+
if (numChars > (unsigned int)INT16_MAX)
|
854
|
+
return LENGTH_OVERFLOW;
|
855
|
+
long result = (long)numChars;
|
856
|
+
if (mode == qrcodegen_Mode_NUMERIC)
|
857
|
+
result = (result * 10 + 2) / 3; // ceil(10/3 * n)
|
858
|
+
else if (mode == qrcodegen_Mode_ALPHANUMERIC)
|
859
|
+
result = (result * 11 + 1) / 2; // ceil(11/2 * n)
|
860
|
+
else if (mode == qrcodegen_Mode_BYTE)
|
861
|
+
result *= 8;
|
862
|
+
else if (mode == qrcodegen_Mode_KANJI)
|
863
|
+
result *= 13;
|
864
|
+
else if (mode == qrcodegen_Mode_ECI && numChars == 0)
|
865
|
+
result = 3 * 8;
|
866
|
+
else { // Invalid argument
|
867
|
+
assert(false);
|
868
|
+
return LENGTH_OVERFLOW;
|
869
|
+
}
|
870
|
+
assert(result >= 0);
|
871
|
+
if (result > INT16_MAX)
|
872
|
+
return LENGTH_OVERFLOW;
|
873
|
+
return (int)result;
|
874
|
+
}
|
875
|
+
|
876
|
+
|
877
|
+
// Public function - see documentation comment in header file.
|
878
|
+
struct qrcodegen_Segment qrcodegen_makeBytes(const uint8_t data[], size_t len, uint8_t buf[]) {
|
879
|
+
assert(data != NULL || len == 0);
|
880
|
+
struct qrcodegen_Segment result;
|
881
|
+
result.mode = qrcodegen_Mode_BYTE;
|
882
|
+
result.bitLength = calcSegmentBitLength(result.mode, len);
|
883
|
+
assert(result.bitLength != LENGTH_OVERFLOW);
|
884
|
+
result.numChars = (int)len;
|
885
|
+
if (len > 0)
|
886
|
+
memcpy(buf, data, len * sizeof(buf[0]));
|
887
|
+
result.data = buf;
|
888
|
+
return result;
|
889
|
+
}
|
890
|
+
|
891
|
+
|
892
|
+
// Public function - see documentation comment in header file.
|
893
|
+
struct qrcodegen_Segment qrcodegen_makeNumeric(const char *digits, uint8_t buf[]) {
|
894
|
+
assert(digits != NULL);
|
895
|
+
struct qrcodegen_Segment result;
|
896
|
+
size_t len = strlen(digits);
|
897
|
+
result.mode = qrcodegen_Mode_NUMERIC;
|
898
|
+
int bitLen = calcSegmentBitLength(result.mode, len);
|
899
|
+
assert(bitLen != LENGTH_OVERFLOW);
|
900
|
+
result.numChars = (int)len;
|
901
|
+
if (bitLen > 0)
|
902
|
+
memset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));
|
903
|
+
result.bitLength = 0;
|
904
|
+
|
905
|
+
unsigned int accumData = 0;
|
906
|
+
int accumCount = 0;
|
907
|
+
for (; *digits != '\0'; digits++) {
|
908
|
+
char c = *digits;
|
909
|
+
assert('0' <= c && c <= '9');
|
910
|
+
accumData = accumData * 10 + (unsigned int)(c - '0');
|
911
|
+
accumCount++;
|
912
|
+
if (accumCount == 3) {
|
913
|
+
appendBitsToBuffer(accumData, 10, buf, &result.bitLength);
|
914
|
+
accumData = 0;
|
915
|
+
accumCount = 0;
|
916
|
+
}
|
917
|
+
}
|
918
|
+
if (accumCount > 0) // 1 or 2 digits remaining
|
919
|
+
appendBitsToBuffer(accumData, accumCount * 3 + 1, buf, &result.bitLength);
|
920
|
+
assert(result.bitLength == bitLen);
|
921
|
+
result.data = buf;
|
922
|
+
return result;
|
923
|
+
}
|
924
|
+
|
925
|
+
|
926
|
+
// Public function - see documentation comment in header file.
|
927
|
+
struct qrcodegen_Segment qrcodegen_makeAlphanumeric(const char *text, uint8_t buf[]) {
|
928
|
+
assert(text != NULL);
|
929
|
+
struct qrcodegen_Segment result;
|
930
|
+
size_t len = strlen(text);
|
931
|
+
result.mode = qrcodegen_Mode_ALPHANUMERIC;
|
932
|
+
int bitLen = calcSegmentBitLength(result.mode, len);
|
933
|
+
assert(bitLen != LENGTH_OVERFLOW);
|
934
|
+
result.numChars = (int)len;
|
935
|
+
if (bitLen > 0)
|
936
|
+
memset(buf, 0, ((size_t)bitLen + 7) / 8 * sizeof(buf[0]));
|
937
|
+
result.bitLength = 0;
|
938
|
+
|
939
|
+
unsigned int accumData = 0;
|
940
|
+
int accumCount = 0;
|
941
|
+
for (; *text != '\0'; text++) {
|
942
|
+
const char *temp = strchr(ALPHANUMERIC_CHARSET, *text);
|
943
|
+
assert(temp != NULL);
|
944
|
+
accumData = accumData * 45 + (unsigned int)(temp - ALPHANUMERIC_CHARSET);
|
945
|
+
accumCount++;
|
946
|
+
if (accumCount == 2) {
|
947
|
+
appendBitsToBuffer(accumData, 11, buf, &result.bitLength);
|
948
|
+
accumData = 0;
|
949
|
+
accumCount = 0;
|
950
|
+
}
|
951
|
+
}
|
952
|
+
if (accumCount > 0) // 1 character remaining
|
953
|
+
appendBitsToBuffer(accumData, 6, buf, &result.bitLength);
|
954
|
+
assert(result.bitLength == bitLen);
|
955
|
+
result.data = buf;
|
956
|
+
return result;
|
957
|
+
}
|
958
|
+
|
959
|
+
|
960
|
+
// Public function - see documentation comment in header file.
|
961
|
+
struct qrcodegen_Segment qrcodegen_makeEci(long assignVal, uint8_t buf[]) {
|
962
|
+
struct qrcodegen_Segment result;
|
963
|
+
result.mode = qrcodegen_Mode_ECI;
|
964
|
+
result.numChars = 0;
|
965
|
+
result.bitLength = 0;
|
966
|
+
if (assignVal < 0)
|
967
|
+
assert(false);
|
968
|
+
else if (assignVal < (1 << 7)) {
|
969
|
+
memset(buf, 0, 1 * sizeof(buf[0]));
|
970
|
+
appendBitsToBuffer((unsigned int)assignVal, 8, buf, &result.bitLength);
|
971
|
+
} else if (assignVal < (1 << 14)) {
|
972
|
+
memset(buf, 0, 2 * sizeof(buf[0]));
|
973
|
+
appendBitsToBuffer(2, 2, buf, &result.bitLength);
|
974
|
+
appendBitsToBuffer((unsigned int)assignVal, 14, buf, &result.bitLength);
|
975
|
+
} else if (assignVal < 1000000L) {
|
976
|
+
memset(buf, 0, 3 * sizeof(buf[0]));
|
977
|
+
appendBitsToBuffer(6, 3, buf, &result.bitLength);
|
978
|
+
appendBitsToBuffer((unsigned int)(assignVal >> 10), 11, buf, &result.bitLength);
|
979
|
+
appendBitsToBuffer((unsigned int)(assignVal & 0x3FF), 10, buf, &result.bitLength);
|
980
|
+
} else
|
981
|
+
assert(false);
|
982
|
+
result.data = buf;
|
983
|
+
return result;
|
984
|
+
}
|
985
|
+
|
986
|
+
|
987
|
+
// Calculates the number of bits needed to encode the given segments at the given version.
|
988
|
+
// Returns a non-negative number if successful. Otherwise returns LENGTH_OVERFLOW if a segment
|
989
|
+
// has too many characters to fit its length field, or the total bits exceeds INT16_MAX.
|
990
|
+
testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version) {
|
991
|
+
assert(segs != NULL || len == 0);
|
992
|
+
long result = 0;
|
993
|
+
for (size_t i = 0; i < len; i++) {
|
994
|
+
int numChars = segs[i].numChars;
|
995
|
+
int bitLength = segs[i].bitLength;
|
996
|
+
assert(0 <= numChars && numChars <= INT16_MAX);
|
997
|
+
assert(0 <= bitLength && bitLength <= INT16_MAX);
|
998
|
+
int ccbits = numCharCountBits(segs[i].mode, version);
|
999
|
+
assert(0 <= ccbits && ccbits <= 16);
|
1000
|
+
if (numChars >= (1L << ccbits))
|
1001
|
+
return LENGTH_OVERFLOW; // The segment's length doesn't fit the field's bit width
|
1002
|
+
result += 4L + ccbits + bitLength;
|
1003
|
+
if (result > INT16_MAX)
|
1004
|
+
return LENGTH_OVERFLOW; // The sum might overflow an int type
|
1005
|
+
}
|
1006
|
+
assert(0 <= result && result <= INT16_MAX);
|
1007
|
+
return (int)result;
|
1008
|
+
}
|
1009
|
+
|
1010
|
+
|
1011
|
+
// Returns the bit width of the character count field for a segment in the given mode
|
1012
|
+
// in a QR Code at the given version number. The result is in the range [0, 16].
|
1013
|
+
static int numCharCountBits(enum qrcodegen_Mode mode, int version) {
|
1014
|
+
assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
|
1015
|
+
int i = (version + 7) / 17;
|
1016
|
+
switch (mode) {
|
1017
|
+
case qrcodegen_Mode_NUMERIC : { static const int temp[] = {10, 12, 14}; return temp[i]; }
|
1018
|
+
case qrcodegen_Mode_ALPHANUMERIC: { static const int temp[] = { 9, 11, 13}; return temp[i]; }
|
1019
|
+
case qrcodegen_Mode_BYTE : { static const int temp[] = { 8, 16, 16}; return temp[i]; }
|
1020
|
+
case qrcodegen_Mode_KANJI : { static const int temp[] = { 8, 10, 12}; return temp[i]; }
|
1021
|
+
case qrcodegen_Mode_ECI : return 0;
|
1022
|
+
default: assert(false); return -1; // Dummy value
|
1023
|
+
}
|
1024
|
+
}
|
1025
|
+
|
1026
|
+
|
1027
|
+
#undef LENGTH_OVERFLOW
|