SWE-350 TOTP Generator Milestone 5
The DE-10 board has six 7-segment displays, this can be used to display and generate a time based one-time pin (TOTP).
Loading...
Searching...
No Matches
QrCode.cpp
Go to the documentation of this file.
1/*
2 * QR Code generator library (C++)
3 *
4 * Copyright (c) Project Nayuki
5 * https://www.nayuki.io/page/qr-code-generator-library
6 *
7 * (MIT License)
8 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12 * the Software, and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
14 * - The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 * - The Software is provided "as is", without warranty of any kind, express or
17 * implied, including but not limited to the warranties of merchantability,
18 * fitness for a particular purpose and noninfringement. In no event shall the
19 * authors or copyright holders be liable for any claim, damages or other
20 * liability, whether in an action of contract, tort or otherwise, arising from,
21 * out of or in connection with the Software or the use or other dealings in the
22 * Software.
23 */
24
25#include <algorithm>
26#include <climits>
27#include <cmath>
28#include <cstddef>
29#include <sstream>
30#include "BitBuffer.hpp"
31#include "QrCode.hpp"
32
33
34qrcodegen::QrCode::Ecc::Ecc(int ord, int fb) :
35 ordinal(ord),
36 formatBits(fb) {}
37
38
43
44
45qrcodegen::QrCode qrcodegen::QrCode::encodeText(const char *text, int version, const Ecc &ecl) {
46 std::vector<QrSegment> segs(QrSegment::makeSegments(text));
47 return encodeSegments(segs, ecl, version, version, -1, false);
48}
49
50
51qrcodegen::QrCode qrcodegen::QrCode::encodeBinary(const std::vector<uint8_t> &data, const Ecc &ecl) {
52 std::vector<QrSegment> segs;
53 segs.push_back(QrSegment::makeBytes(data));
54 return encodeSegments(segs, ecl);
55}
56
57
58qrcodegen::QrCode qrcodegen::QrCode::encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl,
59 int minVersion, int maxVersion, int mask, bool boostEcl) {
60 if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7)
61 throw "Invalid value";
62
63 // Find the minimal version number to use
64 int version, dataUsedBits;
65 for (version = minVersion; ; version++) {
66 int dataCapacityBits = getNumDataCodewords(version, ecl) * 8; // Number of data bits available
67 dataUsedBits = QrSegment::getTotalBits(segs, version);
68 if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits)
69 break; // This version number is found to be suitable
70 if (version >= maxVersion) // All versions in the range could not fit the given data
71 throw "Data too long";
72 }
73 if (dataUsedBits == -1)
74 throw "Assertion error";
75
76 // Increase the error correction level while the data still fits in the current version number
77 const Ecc *newEcl = &ecl;
78 if (boostEcl) {
79 if (dataUsedBits <= getNumDataCodewords(version, Ecc::MEDIUM ) * 8) newEcl = &Ecc::MEDIUM ;
80 if (dataUsedBits <= getNumDataCodewords(version, Ecc::QUARTILE) * 8) newEcl = &Ecc::QUARTILE;
81 if (dataUsedBits <= getNumDataCodewords(version, Ecc::HIGH ) * 8) newEcl = &Ecc::HIGH ;
82 }
83
84 // Create the data bit string by concatenating all segments
85 int dataCapacityBits = getNumDataCodewords(version, *newEcl) * 8;
86 BitBuffer bb;
87 for (size_t i = 0; i < segs.size(); i++) {
88 const QrSegment &seg(segs.at(i));
89 bb.appendBits(seg.mode.modeBits, 4);
90 bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));
91 bb.appendData(seg);
92 }
93
94 // Add terminator and pad up to a byte if applicable
95 bb.appendBits(0, std::min(4, dataCapacityBits - bb.getBitLength()));
96 bb.appendBits(0, (8 - bb.getBitLength() % 8) % 8);
97
98 // Pad with alternate bytes until data capacity is reached
99 for (uint8_t padByte = 0xEC; bb.getBitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
100 bb.appendBits(padByte, 8);
101 if (bb.getBitLength() % 8 != 0)
102 throw "Assertion error";
103
104 // Create the QR Code symbol
105 return QrCode(version, *newEcl, bb.getBytes(), mask);
106}
107
108
109qrcodegen::QrCode::QrCode(int ver, const Ecc &ecl, const std::vector<uint8_t> &dataCodewords, int mask) :
110 // Initialize scalar fields
111 version(ver),
112 size(1 <= ver && ver <= 40 ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior
113 errorCorrectionLevel(ecl) {
114
115 // Check arguments
116 if (ver < 1 || ver > 40 || mask < -1 || mask > 7)
117 throw "Value out of range";
118
119 std::vector<bool> row(size);
120 for (int i = 0; i < size; i++) {
121 modules.push_back(row);
122 isFunction.push_back(row);
123 }
124
125 // Draw function patterns, draw all codewords, do masking
126 drawFunctionPatterns();
127 const std::vector<uint8_t> allCodewords(appendErrorCorrection(dataCodewords));
128 drawCodewords(allCodewords);
129 this->mask = handleConstructorMasking(mask);
130}
131
132
133qrcodegen::QrCode::QrCode(const QrCode &qr, int mask) :
134 // Copy scalar fields
135 version(qr.version),
136 size(qr.size),
137 errorCorrectionLevel(qr.errorCorrectionLevel) {
138
139 // Check arguments
140 if (mask < -1 || mask > 7)
141 throw "Mask value out of range";
142
143 // Handle grid fields
144 modules = qr.modules;
145 isFunction = qr.isFunction;
146
147 // Handle masking
148 applyMask(qr.mask); // Undo old mask
149 this->mask = handleConstructorMasking(mask);
150}
151
152
154 return mask;
155}
156
157
158int qrcodegen::QrCode::getModule(int x, int y) const {
159 if (0 <= x && x < size && 0 <= y && y < size)
160 return modules.at(y).at(x) ? 1 : 0;
161 else
162 return 0; // Infinite white border
163}
164
165
166std::string qrcodegen::QrCode::toSvgString(int border) const {
167 if (border < 0)
168 throw "Border must be non-negative";
169 std::ostringstream sb;
170 sb << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
171 sb << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
172 sb << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ";
173 sb << (size + border * 2) << " " << (size + border * 2) << "\">\n";
174 sb << "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\" stroke-width=\"0\"/>\n";
175 sb << "\t<path d=\"";
176 bool head = true;
177 for (int y = -border; y < size + border; y++) {
178 for (int x = -border; x < size + border; x++) {
179 if (getModule(x, y) == 1) {
180 if (head)
181 head = false;
182 else
183 sb << " ";
184 sb << "M" << (x + border) << "," << (y + border) << "h1v1h-1z";
185 }
186 }
187 }
188 sb << "\" fill=\"#000000\" stroke-width=\"0\"/>\n";
189 sb << "</svg>\n";
190 return sb.str();
191}
192
193
194void qrcodegen::QrCode::drawFunctionPatterns() {
195 // Draw the horizontal and vertical timing patterns
196 for (int i = 0; i < size; i++) {
197 setFunctionModule(6, i, i % 2 == 0);
198 setFunctionModule(i, 6, i % 2 == 0);
199 }
200
201 // Draw 3 finder patterns (all corners except bottom right; overwrites some timing modules)
202 drawFinderPattern(3, 3);
203 drawFinderPattern(size - 4, 3);
204 drawFinderPattern(3, size - 4);
205
206 // Draw the numerous alignment patterns
207 const std::vector<int> alignPatPos(getAlignmentPatternPositions(version));
208 int numAlign = alignPatPos.size();
209 for (int i = 0; i < numAlign; i++) {
210 for (int j = 0; j < numAlign; j++) {
211 if ((i == 0 && j == 0) || (i == 0 && j == numAlign - 1) || (i == numAlign - 1 && j == 0))
212 continue; // Skip the three finder corners
213 else
214 drawAlignmentPattern(alignPatPos.at(i), alignPatPos.at(j));
215 }
216 }
217
218 // Draw configuration data
219 drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
220 drawVersion();
221}
222
223
224void qrcodegen::QrCode::drawFormatBits(int mask) {
225 // Calculate error correction code and pack bits
226 int data = errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3
227 int rem = data;
228 for (int i = 0; i < 10; i++)
229 rem = (rem << 1) ^ ((rem >> 9) * 0x537);
230 data = data << 10 | rem;
231 data ^= 0x5412; // uint15
232 if (data >> 15 != 0)
233 throw "Assertion error";
234
235 // Draw first copy
236 for (int i = 0; i <= 5; i++)
237 setFunctionModule(8, i, ((data >> i) & 1) != 0);
238 setFunctionModule(8, 7, ((data >> 6) & 1) != 0);
239 setFunctionModule(8, 8, ((data >> 7) & 1) != 0);
240 setFunctionModule(7, 8, ((data >> 8) & 1) != 0);
241 for (int i = 9; i < 15; i++)
242 setFunctionModule(14 - i, 8, ((data >> i) & 1) != 0);
243
244 // Draw second copy
245 for (int i = 0; i <= 7; i++)
246 setFunctionModule(size - 1 - i, 8, ((data >> i) & 1) != 0);
247 for (int i = 8; i < 15; i++)
248 setFunctionModule(8, size - 15 + i, ((data >> i) & 1) != 0);
249 setFunctionModule(8, size - 8, true);
250}
251
252
253void qrcodegen::QrCode::drawVersion() {
254 if (version < 7)
255 return;
256
257 // Calculate error correction code and pack bits
258 int rem = version; // version is uint6, in the range [7, 40]
259 for (int i = 0; i < 12; i++)
260 rem = (rem << 1) ^ ((rem >> 11) * 0x1F25);
261 int data = version << 12 | rem; // uint18
262 if (data >> 18 != 0)
263 throw "Assertion error";
264
265 // Draw two copies
266 for (int i = 0; i < 18; i++) {
267 bool bit = ((data >> i) & 1) != 0;
268 int a = size - 11 + i % 3, b = i / 3;
269 setFunctionModule(a, b, bit);
270 setFunctionModule(b, a, bit);
271 }
272}
273
274
275void qrcodegen::QrCode::drawFinderPattern(int x, int y) {
276 for (int i = -4; i <= 4; i++) {
277 for (int j = -4; j <= 4; j++) {
278 int dist = std::max(std::abs(i), std::abs(j)); // Chebyshev/infinity norm
279 int xx = x + j, yy = y + i;
280 if (0 <= xx && xx < size && 0 <= yy && yy < size)
281 setFunctionModule(xx, yy, dist != 2 && dist != 4);
282 }
283 }
284}
285
286
287void qrcodegen::QrCode::drawAlignmentPattern(int x, int y) {
288 for (int i = -2; i <= 2; i++) {
289 for (int j = -2; j <= 2; j++)
290 setFunctionModule(x + j, y + i, std::max(std::abs(i), std::abs(j)) != 1);
291 }
292}
293
294
295void qrcodegen::QrCode::setFunctionModule(int x, int y, bool isBlack) {
296 modules.at(y).at(x) = isBlack;
297 isFunction.at(y).at(x) = true;
298}
299
300
301std::vector<uint8_t> qrcodegen::QrCode::appendErrorCorrection(const std::vector<uint8_t> &data) const {
302 if (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))
303 throw "Invalid argument";
304
305 // Calculate parameter numbers
306 int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal][version];
307 int totalEcc = NUM_ERROR_CORRECTION_CODEWORDS[errorCorrectionLevel.ordinal][version];
308 if (totalEcc % numBlocks != 0)
309 throw "Assertion error";
310 int blockEccLen = totalEcc / numBlocks;
311 int numShortBlocks = numBlocks - getNumRawDataModules(version) / 8 % numBlocks;
312 int shortBlockLen = getNumRawDataModules(version) / 8 / numBlocks;
313
314 // Split data into blocks and append ECC to each block
315 std::vector<std::vector<uint8_t> > blocks;
316 const ReedSolomonGenerator rs(blockEccLen);
317 for (int i = 0, k = 0; i < numBlocks; i++) {
318 std::vector<uint8_t> dat;
319 dat.insert(dat.begin(), data.begin() + k, data.begin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));
320 k += dat.size();
321 const std::vector<uint8_t> ecc(rs.getRemainder(dat));
322 if (i < numShortBlocks)
323 dat.push_back(0);
324 dat.insert(dat.end(), ecc.begin(), ecc.end());
325 blocks.push_back(dat);
326 }
327
328 // Interleave (not concatenate) the bytes from every block into a single sequence
329 std::vector<uint8_t> result;
330 for (int i = 0; static_cast<unsigned int>(i) < blocks.at(0).size(); i++) {
331 for (int j = 0; static_cast<unsigned int>(j) < blocks.size(); j++) {
332 // Skip the padding byte in short blocks
333 if (i != shortBlockLen - blockEccLen || j >= numShortBlocks)
334 result.push_back(blocks.at(j).at(i));
335 }
336 }
337 if (result.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))
338 throw "Assertion error";
339 return result;
340}
341
342
343void qrcodegen::QrCode::drawCodewords(const std::vector<uint8_t> &data) {
344 if (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))
345 throw "Invalid argument";
346
347 size_t i = 0; // Bit index into the data
348 // Do the funny zigzag scan
349 for (int right = size - 1; right >= 1; right -= 2) { // Index of right column in each column pair
350 if (right == 6)
351 right = 5;
352 for (int vert = 0; vert < size; vert++) { // Vertical counter
353 for (int j = 0; j < 2; j++) {
354 int x = right - j; // Actual x coordinate
355 bool upwards = ((right & 2) == 0) ^ (x < 6);
356 int y = upwards ? size - 1 - vert : vert; // Actual y coordinate
357 if (!isFunction.at(y).at(x) && i < data.size() * 8) {
358 modules.at(y).at(x) = ((data.at(i >> 3) >> (7 - (i & 7))) & 1) != 0;
359 i++;
360 }
361 // If there are any remainder bits (0 to 7), they are already
362 // set to 0/false/white when the grid of modules was initialized
363 }
364 }
365 }
366 if (static_cast<unsigned int>(i) != data.size() * 8)
367 throw "Assertion error";
368}
369
370
371void qrcodegen::QrCode::applyMask(int mask) {
372 if (mask < 0 || mask > 7)
373 throw "Mask value out of range";
374 for (int y = 0; y < size; y++) {
375 for (int x = 0; x < size; x++) {
376 bool invert;
377 switch (mask) {
378 case 0: invert = (x + y) % 2 == 0; break;
379 case 1: invert = y % 2 == 0; break;
380 case 2: invert = x % 3 == 0; break;
381 case 3: invert = (x + y) % 3 == 0; break;
382 case 4: invert = (x / 3 + y / 2) % 2 == 0; break;
383 case 5: invert = x * y % 2 + x * y % 3 == 0; break;
384 case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break;
385 case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break;
386 default: throw "Assertion error";
387 }
388 modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));
389 }
390 }
391}
392
393
394int qrcodegen::QrCode::handleConstructorMasking(int mask) {
395 if (mask == -1) { // Automatically choose best mask
396 int32_t minPenalty = INT32_MAX;
397 for (int i = 0; i < 8; i++) {
398 drawFormatBits(i);
399 applyMask(i);
400 int penalty = getPenaltyScore();
401 if (penalty < minPenalty) {
402 mask = i;
403 minPenalty = penalty;
404 }
405 applyMask(i); // Undoes the mask due to XOR
406 }
407 }
408 if (mask < 0 || mask > 7)
409 throw "Assertion error";
410 drawFormatBits(mask); // Overwrite old format bits
411 applyMask(mask); // Apply the final choice of mask
412 return mask; // The caller shall assign this value to the final-declared field
413}
414
415
416int qrcodegen::QrCode::getPenaltyScore() const {
417 int result = 0;
418
419 // Adjacent modules in row having same color
420 for (int y = 0; y < size; y++) {
421 bool colorX = modules.at(y).at(0);
422 for (int x = 1, runX = 1; x < size; x++) {
423 if (modules.at(y).at(x) != colorX) {
424 colorX = modules.at(y).at(x);
425 runX = 1;
426 } else {
427 runX++;
428 if (runX == 5)
430 else if (runX > 5)
431 result++;
432 }
433 }
434 }
435 // Adjacent modules in column having same color
436 for (int x = 0; x < size; x++) {
437 bool colorY = modules.at(0).at(x);
438 for (int y = 1, runY = 1; y < size; y++) {
439 if (modules.at(y).at(x) != colorY) {
440 colorY = modules.at(y).at(x);
441 runY = 1;
442 } else {
443 runY++;
444 if (runY == 5)
446 else if (runY > 5)
447 result++;
448 }
449 }
450 }
451
452 // 2*2 blocks of modules having same color
453 for (int y = 0; y < size - 1; y++) {
454 for (int x = 0; x < size - 1; x++) {
455 bool color = modules.at(y).at(x);
456 if ( color == modules.at(y).at(x + 1) &&
457 color == modules.at(y + 1).at(x) &&
458 color == modules.at(y + 1).at(x + 1))
460 }
461 }
462
463 // Finder-like pattern in rows
464 for (int y = 0; y < size; y++) {
465 for (int x = 0, bits = 0; x < size; x++) {
466 bits = ((bits << 1) & 0x7FF) | (modules.at(y).at(x) ? 1 : 0);
467 if (x >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
469 }
470 }
471 // Finder-like pattern in columns
472 for (int x = 0; x < size; x++) {
473 for (int y = 0, bits = 0; y < size; y++) {
474 bits = ((bits << 1) & 0x7FF) | (modules.at(y).at(x) ? 1 : 0);
475 if (y >= 10 && (bits == 0x05D || bits == 0x5D0)) // Needs 11 bits accumulated
477 }
478 }
479
480 // Balance of black and white modules
481 int black = 0;
482 for (int y = 0; y < size; y++) {
483 for (int x = 0; x < size; x++) {
484 if (modules.at(y).at(x))
485 black++;
486 }
487 }
488 int total = size * size;
489 // Find smallest k such that (45-5k)% <= dark/total <= (55+5k)%
490 for (int k = 0; black*20 < (9-k)*total || black*20 > (11+k)*total; k++)
492 return result;
493}
494
495
496std::vector<int> qrcodegen::QrCode::getAlignmentPatternPositions(int ver) {
497 if (ver < 1 || ver > 40)
498 throw "Version number out of range";
499 else if (ver == 1)
500 return std::vector<int>();
501 else {
502 int numAlign = ver / 7 + 2;
503 int step;
504 if (ver != 32)
505 step = (ver * 4 + numAlign * 2 + 1) / (2 * numAlign - 2) * 2; // ceil((size - 13) / (2*numAlign - 2)) * 2
506 else // C-C-C-Combo breaker!
507 step = 26;
508
509 std::vector<int> result;
510 int size = ver * 4 + 17;
511 for (int i = 0, pos = size - 7; i < numAlign - 1; i++, pos -= step)
512 result.insert(result.begin(), pos);
513 result.insert(result.begin(), 6);
514 return result;
515 }
516}
517
518
519int qrcodegen::QrCode::getNumRawDataModules(int ver) {
520 if (ver < 1 || ver > 40)
521 throw "Version number out of range";
522 int result = (16 * ver + 128) * ver + 64;
523 if (ver >= 2) {
524 int numAlign = ver / 7 + 2;
525 result -= (25 * numAlign - 10) * numAlign - 55;
526 if (ver >= 7)
527 result -= 18 * 2; // Subtract version information
528 }
529 return result;
530}
531
532
533int qrcodegen::QrCode::getNumDataCodewords(int ver, const Ecc &ecl) {
534 if (ver < 1 || ver > 40)
535 throw "Version number out of range";
536 return getNumRawDataModules(ver) / 8 - NUM_ERROR_CORRECTION_CODEWORDS[ecl.ordinal][ver];
537}
538
539
540/*---- Tables of constants ----*/
541
542const int qrcodegen::QrCode::PENALTY_N1 = 3;
543const int qrcodegen::QrCode::PENALTY_N2 = 3;
544const int qrcodegen::QrCode::PENALTY_N3 = 40;
545const int qrcodegen::QrCode::PENALTY_N4 = 10;
546
547
548const int16_t qrcodegen::QrCode::NUM_ERROR_CORRECTION_CODEWORDS[4][41] = {
549 // Version: (note that index 0 is for padding, and is set to an illegal value)
550 //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
551 {-1, 7, 10, 15, 20, 26, 36, 40, 48, 60, 72, 80, 96, 104, 120, 132, 144, 168, 180, 196, 224, 224, 252, 270, 300, 312, 336, 360, 390, 420, 450, 480, 510, 540, 570, 570, 600, 630, 660, 720, 750}, // Low
552 {-1, 10, 16, 26, 36, 48, 64, 72, 88, 110, 130, 150, 176, 198, 216, 240, 280, 308, 338, 364, 416, 442, 476, 504, 560, 588, 644, 700, 728, 784, 812, 868, 924, 980, 1036, 1064, 1120, 1204, 1260, 1316, 1372}, // Medium
553 {-1, 13, 22, 36, 52, 72, 96, 108, 132, 160, 192, 224, 260, 288, 320, 360, 408, 448, 504, 546, 600, 644, 690, 750, 810, 870, 952, 1020, 1050, 1140, 1200, 1290, 1350, 1440, 1530, 1590, 1680, 1770, 1860, 1950, 2040}, // Quartile
554 {-1, 17, 28, 44, 64, 88, 112, 130, 156, 192, 224, 264, 308, 352, 384, 432, 480, 532, 588, 650, 700, 750, 816, 900, 960, 1050, 1110, 1200, 1260, 1350, 1440, 1530, 1620, 1710, 1800, 1890, 1980, 2100, 2220, 2310, 2430}, // High
555};
556
557const int8_t qrcodegen::QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
558 // Version: (note that index 0 is for padding, and is set to an illegal value)
559 //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
560 {-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
561 {-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
562 {-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
563 {-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
564};
565
566
567qrcodegen::QrCode::ReedSolomonGenerator::ReedSolomonGenerator(int degree) :
568 coefficients() {
569 if (degree < 1 || degree > 255)
570 throw "Degree out of range";
571
572 // Start with the monomial x^0
573 coefficients.resize(degree);
574 coefficients.at(degree - 1) = 1;
575
576 // Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
577 // drop the highest term, and store the rest of the coefficients in order of descending powers.
578 // Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
579 int root = 1;
580 for (int i = 0; i < degree; i++) {
581 // Multiply the current product by (x - r^i)
582 for (size_t j = 0; j < coefficients.size(); j++) {
583 coefficients.at(j) = multiply(coefficients.at(j), static_cast<uint8_t>(root));
584 if (j + 1 < coefficients.size())
585 coefficients.at(j) ^= coefficients.at(j + 1);
586 }
587 root = (root << 1) ^ ((root >> 7) * 0x11D); // Multiply by 0x02 mod GF(2^8/0x11D)
588 }
589}
590
591
592std::vector<uint8_t> qrcodegen::QrCode::ReedSolomonGenerator::getRemainder(const std::vector<uint8_t> &data) const {
593 // Compute the remainder by performing polynomial division
594 std::vector<uint8_t> result(coefficients.size());
595 for (size_t i = 0; i < data.size(); i++) {
596 uint8_t factor = data.at(i) ^ result.at(0);
597 result.erase(result.begin());
598 result.push_back(0);
599 for (size_t j = 0; j < result.size(); j++)
600 result.at(j) ^= multiply(coefficients.at(j), factor);
601 }
602 return result;
603}
604
605
606uint8_t qrcodegen::QrCode::ReedSolomonGenerator::multiply(uint8_t x, uint8_t y) {
607 // Russian peasant multiplication
608 int z = 0;
609 for (int i = 7; i >= 0; i--) {
610 z = (z << 1) ^ ((z >> 7) * 0x11D);
611 z ^= ((y >> i) & 1) * x;
612 }
613 if (z >> 8 != 0)
614 throw "Assertion error";
615 return static_cast<uint8_t>(z);
616}
std::vector< uint8_t > getBytes() const
Definition BitBuffer.cpp:39
void appendData(const QrSegment &seg)
Definition BitBuffer.cpp:55
int getBitLength() const
Definition BitBuffer.cpp:34
void appendBits(uint32_t val, int len)
Definition BitBuffer.cpp:44
static const Ecc LOW
Definition QrCode.hpp:52
static const Ecc QUARTILE
Definition QrCode.hpp:52
static const Ecc HIGH
Definition QrCode.hpp:52
static const Ecc MEDIUM
Definition QrCode.hpp:52
QrCode(int ver, const Ecc &ecl, const std::vector< uint8_t > &dataCodewords, int mask)
Definition QrCode.cpp:109
static QrCode encodeText(const char *text, int version, const Ecc &ecl)
Definition QrCode.cpp:45
int getMask() const
Definition QrCode.cpp:153
const int size
Definition QrCode.hpp:109
const int version
Definition QrCode.hpp:105
static QrCode encodeBinary(const std::vector< uint8_t > &data, const Ecc &ecl)
Definition QrCode.cpp:51
int getModule(int x, int y) const
Definition QrCode.cpp:158
std::string toSvgString(int border) const
Definition QrCode.cpp:166
static QrCode encodeSegments(const std::vector< QrSegment > &segs, const Ecc &ecl, int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true)
Definition QrCode.cpp:58
int numCharCountBits(int ver) const
Definition QrSegment.cpp:38
static std::vector< QrSegment > makeSegments(const char *text)
static int getTotalBits(const std::vector< QrSegment > &segs, int version)
static QrSegment makeBytes(const std::vector< uint8_t > &data)
Definition QrSegment.cpp:53
#define PENALTY_N3
Definition qrcode.c:482
#define PENALTY_N4
Definition qrcode.c:483
#define PENALTY_N2
Definition qrcode.c:481
#define PENALTY_N1
Definition qrcode.c:480
uint8_t * result(void)
Definition sha1.c:104