2016-04-15 04:26:37 +00:00
|
|
|
/*
|
|
|
|
* QR Code generator library (C++)
|
|
|
|
*
|
2017-04-24 20:41:54 +00:00
|
|
|
* Copyright (c) Project Nayuki. (MIT License)
|
2016-04-15 04:26:37 +00:00
|
|
|
* https://www.nayuki.io/page/qr-code-generator-library
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
* - The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
* - The Software is provided "as is", without warranty of any kind, express or
|
|
|
|
* implied, including but not limited to the warranties of merchantability,
|
|
|
|
* fitness for a particular purpose and noninfringement. In no event shall the
|
|
|
|
* authors or copyright holders be liable for any claim, damages or other
|
|
|
|
* liability, whether in an action of contract, tort or otherwise, arising from,
|
|
|
|
* out of or in connection with the Software or the use or other dealings in the
|
|
|
|
* Software.
|
|
|
|
*/
|
|
|
|
|
2017-04-17 16:32:14 +00:00
|
|
|
#include <climits>
|
2017-05-08 06:10:56 +00:00
|
|
|
#include <cstring>
|
2017-08-18 03:43:28 +00:00
|
|
|
#include <utility>
|
2016-04-15 04:26:37 +00:00
|
|
|
#include "QrSegment.hpp"
|
|
|
|
|
2017-05-05 20:57:13 +00:00
|
|
|
using std::uint8_t;
|
2017-05-05 21:03:08 +00:00
|
|
|
using std::vector;
|
2017-05-05 20:57:13 +00:00
|
|
|
|
2016-04-15 04:26:37 +00:00
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
namespace qrcodegen {
|
|
|
|
|
|
|
|
QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
|
2016-04-15 04:26:37 +00:00
|
|
|
modeBits(mode) {
|
|
|
|
numBitsCharCount[0] = cc0;
|
|
|
|
numBitsCharCount[1] = cc1;
|
|
|
|
numBitsCharCount[2] = cc2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 03:37:43 +00:00
|
|
|
int QrSegment::Mode::getModeBits() const {
|
|
|
|
return modeBits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
int QrSegment::Mode::numCharCountBits(int ver) const {
|
2016-04-15 04:26:37 +00:00
|
|
|
if ( 1 <= ver && ver <= 9) return numBitsCharCount[0];
|
|
|
|
else if (10 <= ver && ver <= 26) return numBitsCharCount[1];
|
|
|
|
else if (27 <= ver && ver <= 40) return numBitsCharCount[2];
|
|
|
|
else throw "Version number out of range";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
const QrSegment::Mode QrSegment::Mode::NUMERIC (0x1, 10, 12, 14);
|
|
|
|
const QrSegment::Mode QrSegment::Mode::ALPHANUMERIC(0x2, 9, 11, 13);
|
|
|
|
const QrSegment::Mode QrSegment::Mode::BYTE (0x4, 8, 16, 16);
|
|
|
|
const QrSegment::Mode QrSegment::Mode::KANJI (0x8, 8, 10, 12);
|
2017-05-08 18:04:10 +00:00
|
|
|
const QrSegment::Mode QrSegment::Mode::ECI (0x7, 0, 0, 0);
|
2016-04-15 04:26:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-05 21:03:08 +00:00
|
|
|
QrSegment QrSegment::makeBytes(const vector<uint8_t> &data) {
|
2017-08-18 04:06:11 +00:00
|
|
|
if (data.size() > INT_MAX)
|
|
|
|
throw "Data too long";
|
2017-08-17 21:29:15 +00:00
|
|
|
BitBuffer bb;
|
|
|
|
for (uint8_t b : data)
|
|
|
|
bb.appendBits(b, 8);
|
2017-08-18 04:06:11 +00:00
|
|
|
return QrSegment(Mode::BYTE, static_cast<int>(data.size()), std::move(bb));
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
QrSegment QrSegment::makeNumeric(const char *digits) {
|
2016-04-15 04:26:37 +00:00
|
|
|
BitBuffer bb;
|
|
|
|
int accumData = 0;
|
|
|
|
int accumCount = 0;
|
|
|
|
int charCount = 0;
|
|
|
|
for (; *digits != '\0'; digits++, charCount++) {
|
|
|
|
char c = *digits;
|
|
|
|
if (c < '0' || c > '9')
|
|
|
|
throw "String contains non-numeric characters";
|
|
|
|
accumData = accumData * 10 + (c - '0');
|
|
|
|
accumCount++;
|
|
|
|
if (accumCount == 3) {
|
|
|
|
bb.appendBits(accumData, 10);
|
|
|
|
accumData = 0;
|
|
|
|
accumCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (accumCount > 0) // 1 or 2 digits remaining
|
|
|
|
bb.appendBits(accumData, accumCount * 3 + 1);
|
2017-08-18 03:43:28 +00:00
|
|
|
return QrSegment(Mode::NUMERIC, charCount, std::move(bb));
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
QrSegment QrSegment::makeAlphanumeric(const char *text) {
|
2016-04-15 04:26:37 +00:00
|
|
|
BitBuffer bb;
|
|
|
|
int accumData = 0;
|
|
|
|
int accumCount = 0;
|
|
|
|
int charCount = 0;
|
|
|
|
for (; *text != '\0'; text++, charCount++) {
|
2017-05-08 06:10:56 +00:00
|
|
|
const char *temp = std::strchr(ALPHANUMERIC_CHARSET, *text);
|
|
|
|
if (temp == nullptr)
|
2016-04-15 04:26:37 +00:00
|
|
|
throw "String contains unencodable characters in alphanumeric mode";
|
2017-05-08 06:10:56 +00:00
|
|
|
accumData = accumData * 45 + (temp - ALPHANUMERIC_CHARSET);
|
2016-04-15 04:26:37 +00:00
|
|
|
accumCount++;
|
|
|
|
if (accumCount == 2) {
|
|
|
|
bb.appendBits(accumData, 11);
|
|
|
|
accumData = 0;
|
|
|
|
accumCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (accumCount > 0) // 1 character remaining
|
|
|
|
bb.appendBits(accumData, 6);
|
2017-08-18 03:43:28 +00:00
|
|
|
return QrSegment(Mode::ALPHANUMERIC, charCount, std::move(bb));
|
2016-04-15 04:26:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<QrSegment> QrSegment::makeSegments(const char *text) {
|
2016-04-16 01:44:24 +00:00
|
|
|
// Select the most efficient segment encoding automatically
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<QrSegment> result;
|
2017-08-26 18:57:52 +00:00
|
|
|
if (*text == '\0'); // Leave result empty
|
2017-08-18 00:11:23 +00:00
|
|
|
else if (isNumeric(text))
|
|
|
|
result.push_back(makeNumeric(text));
|
|
|
|
else if (isAlphanumeric(text))
|
|
|
|
result.push_back(makeAlphanumeric(text));
|
2016-04-16 01:44:24 +00:00
|
|
|
else {
|
2017-05-05 21:03:08 +00:00
|
|
|
vector<uint8_t> bytes;
|
2016-04-16 01:44:24 +00:00
|
|
|
for (; *text != '\0'; text++)
|
|
|
|
bytes.push_back(static_cast<uint8_t>(*text));
|
2017-08-18 00:11:23 +00:00
|
|
|
result.push_back(makeBytes(bytes));
|
2016-04-16 01:44:24 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-08 18:04:10 +00:00
|
|
|
QrSegment QrSegment::makeEci(long assignVal) {
|
2017-08-17 21:29:15 +00:00
|
|
|
BitBuffer bb;
|
2017-05-08 18:04:10 +00:00
|
|
|
if (0 <= assignVal && assignVal < (1 << 7))
|
2017-08-17 21:29:15 +00:00
|
|
|
bb.appendBits(assignVal, 8);
|
|
|
|
else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) {
|
|
|
|
bb.appendBits(2, 2);
|
|
|
|
bb.appendBits(assignVal, 14);
|
2017-08-26 18:43:54 +00:00
|
|
|
} else if ((1 << 14) <= assignVal && assignVal < 1000000L) {
|
2017-08-17 21:29:15 +00:00
|
|
|
bb.appendBits(6, 3);
|
|
|
|
bb.appendBits(assignVal, 21);
|
|
|
|
} else
|
2017-05-08 18:04:10 +00:00
|
|
|
throw "ECI assignment value out of range";
|
2017-08-18 03:43:28 +00:00
|
|
|
return QrSegment(Mode::ECI, 0, std::move(bb));
|
2017-05-08 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 04:06:57 +00:00
|
|
|
QrSegment::QrSegment(Mode md, int numCh, const std::vector<bool> &dt) :
|
2016-04-15 04:26:37 +00:00
|
|
|
mode(md),
|
|
|
|
numChars(numCh),
|
2017-08-17 21:49:53 +00:00
|
|
|
data(dt) {
|
|
|
|
if (numCh < 0)
|
2016-04-15 04:26:37 +00:00
|
|
|
throw "Invalid value";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 04:06:57 +00:00
|
|
|
QrSegment::QrSegment(Mode md, int numCh, std::vector<bool> &&dt) :
|
2017-08-18 03:43:28 +00:00
|
|
|
mode(md),
|
|
|
|
numChars(numCh),
|
|
|
|
data(std::move(dt)) {
|
|
|
|
if (numCh < 0)
|
|
|
|
throw "Invalid value";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-05 21:03:08 +00:00
|
|
|
int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {
|
2016-04-16 03:53:58 +00:00
|
|
|
if (version < 1 || version > 40)
|
|
|
|
throw "Version number out of range";
|
|
|
|
int result = 0;
|
2017-08-17 21:04:21 +00:00
|
|
|
for (const QrSegment &seg : segs) {
|
2016-04-16 03:53:58 +00:00
|
|
|
int ccbits = seg.mode.numCharCountBits(version);
|
|
|
|
// Fail if segment length value doesn't fit in the length field's bit-width
|
2017-08-18 00:09:51 +00:00
|
|
|
if (seg.numChars >= (1L << ccbits))
|
2016-04-16 03:53:58 +00:00
|
|
|
return -1;
|
2017-08-18 00:09:51 +00:00
|
|
|
if (4 + ccbits > INT_MAX - result)
|
2017-04-17 16:32:14 +00:00
|
|
|
return -1;
|
2017-08-18 00:09:51 +00:00
|
|
|
result += 4 + ccbits;
|
|
|
|
if (seg.data.size() > static_cast<unsigned int>(INT_MAX - result))
|
|
|
|
return -1;
|
|
|
|
result += static_cast<int>(seg.data.size());
|
2016-04-16 03:53:58 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
bool QrSegment::isAlphanumeric(const char *text) {
|
2016-04-15 04:26:37 +00:00
|
|
|
for (; *text != '\0'; text++) {
|
2017-05-08 06:10:56 +00:00
|
|
|
if (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr)
|
2016-04-15 04:26:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 05:30:59 +00:00
|
|
|
bool QrSegment::isNumeric(const char *text) {
|
2016-04-15 04:26:37 +00:00
|
|
|
for (; *text != '\0'; text++) {
|
|
|
|
char c = *text;
|
|
|
|
if (c < '0' || c > '9')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-06 03:56:06 +00:00
|
|
|
QrSegment::Mode QrSegment::getMode() const {
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int QrSegment::getNumChars() const {
|
|
|
|
return numChars;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::vector<bool> &QrSegment::getData() const {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-08 06:10:56 +00:00
|
|
|
const char *QrSegment::ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
2017-04-21 05:30:59 +00:00
|
|
|
|
|
|
|
}
|