Merge pull request 'formated code' (#13) from formated-code into main
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
Reviewed-on: https://gitea.home.com:3000/TolaMironcenko/openrouter/pulls/13
This commit was merged in pull request #13.
This commit is contained in:
commit
c86d4d77ba
40 files changed
+6548
-5650
No files matched your search
@@ -0,0 +1,46 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
tab_width = 8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[DEVELOPERS]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = tab
|
||||||
|
|
||||||
|
[Config*.in*]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = tab
|
||||||
|
|
||||||
|
[linux/Config.ext.in]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = tab
|
||||||
|
|
||||||
|
[Makefile*]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = tab
|
||||||
|
|
||||||
|
[*.mk]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = tab
|
||||||
|
|
||||||
|
[*.patch]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
|
||||||
|
[S{0..9}{0..9}*]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = tab
|
||||||
|
|
||||||
|
[*.adoc]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
|
||||||
|
[*.{c++,cc,cpp,cxx,h,h++,hh,hpp,hxx,inl,ipp,tlh,tli}]
|
||||||
|
|
||||||
|
cpp_indent_case_contents_when_block = true
|
||||||
|
cpp_new_line_before_open_brace_namespace = new_line
|
||||||
|
cpp_new_line_before_open_brace_function = new_line
|
||||||
@@ -1,13 +1,37 @@
|
|||||||
CXX=g++
|
CXX=g++
|
||||||
STRIP=strip
|
STRIP=strip
|
||||||
CXXFLAGS=-Wall -I src/libs -I src -std=c++20
|
CXXFLAGS=-Wall -I src/libs -I src -std=c++20
|
||||||
SOURCES=src/openrouter.cpp src/security/cors.cpp src/settings/telnet/enabled.cpp src/settings/telnet/port.cpp src/settings/ssh/status.cpp src/settings/ssh/keys.cpp src/settings/ssh/port.cpp src/settings/ssh/server.cpp src/settings/dns/enabled.cpp src/settings/dns/servers.cpp src/settings/dns/local_domains.cpp src/settings/wifi/status.cpp src/settings/wifi/name.cpp src/settings/wifi/password.cpp src/settings/wifi/qr.cpp src/settings/wifi/channel.cpp src/settings/ftp/ftp.cpp src/system/reboot/reboot.cpp src/system/syslog/syslog.cpp src/system/uptime/uptime.cpp src/OpenRouter/OpenRouter.cpp src/auth/auth.cpp src/libs/QR-Code-generator/qrcodegen.cpp src/libs/QR-Code-generator/qrtosvg.cpp
|
SOURCES=src/openrouter.cpp \
|
||||||
TARGET=bin/openrouter
|
src/security/cors.cpp \
|
||||||
|
src/settings/telnet/enabled.cpp \
|
||||||
|
src/settings/telnet/port.cpp \
|
||||||
|
src/settings/ssh/status.cpp \
|
||||||
|
src/settings/ssh/keys.cpp \
|
||||||
|
src/settings/ssh/port.cpp \
|
||||||
|
src/settings/ssh/server.cpp \
|
||||||
|
src/settings/dns/enabled.cpp \
|
||||||
|
src/settings/dns/servers.cpp \
|
||||||
|
src/settings/dns/local_domains.cpp \
|
||||||
|
src/settings/wifi/status.cpp \
|
||||||
|
src/settings/wifi/name.cpp \
|
||||||
|
src/settings/wifi/password.cpp \
|
||||||
|
src/settings/wifi/qr.cpp \
|
||||||
|
src/settings/wifi/channel.cpp \
|
||||||
|
src/settings/ftp/ftp.cpp \
|
||||||
|
src/system/reboot/reboot.cpp \
|
||||||
|
src/system/syslog/syslog.cpp \
|
||||||
|
src/system/uptime/uptime.cpp \
|
||||||
|
src/OpenRouter/OpenRouter.cpp \
|
||||||
|
src/auth/auth.cpp \
|
||||||
|
src/libs/QR-Code-generator/qrcodegen.cpp \
|
||||||
|
src/libs/QR-Code-generator/qrtosvg.cpp
|
||||||
|
TARGET=build/openrouter
|
||||||
default: all
|
default: all
|
||||||
all: build
|
all: build
|
||||||
|
.PHONY: build
|
||||||
build:
|
build:
|
||||||
@if [ ! -d bin ]; then \
|
@if [ ! -d build ]; then \
|
||||||
mkdir bin; \
|
mkdir build; \
|
||||||
fi
|
fi
|
||||||
$(CXX) -o $(TARGET) $(SOURCES) $(CXXFLAGS)
|
$(CXX) -o $(TARGET) $(SOURCES) $(CXXFLAGS)
|
||||||
$(STRIP) $(TARGET)
|
$(STRIP) $(TARGET)
|
||||||
|
|||||||
+3
-8
@@ -1,4 +1,4 @@
|
|||||||
project('openrouter', 'cpp', default_options : ['cpp_std=c++20', 'cpp_args=-Wall'])
|
project('openrouter', 'cpp', default_options: ['cpp_std=c++20', 'cpp_args=-Wall'])
|
||||||
include_dirs = include_directories('src', 'src/libs')
|
include_dirs = include_directories('src', 'src/libs')
|
||||||
executable(
|
executable(
|
||||||
'openrouter',
|
'openrouter',
|
||||||
@@ -33,12 +33,7 @@ executable(
|
|||||||
'src/libs/QR-Code-generator/qrcodegen.cpp',
|
'src/libs/QR-Code-generator/qrcodegen.cpp',
|
||||||
'src/libs/QR-Code-generator/qrtosvg.cpp',
|
'src/libs/QR-Code-generator/qrtosvg.cpp',
|
||||||
include_directories: include_dirs,
|
include_directories: include_dirs,
|
||||||
install: true
|
install: true,
|
||||||
)
|
)
|
||||||
|
|
||||||
install_subdir(
|
install_subdir('src/web', install_dir: 'share/openrouter')
|
||||||
'src/web',
|
|
||||||
install_dir: 'share/openrouter'
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ OpenRouter::OpenRouter()
|
|||||||
|
|
||||||
OpenRouter::~OpenRouter() = default;
|
OpenRouter::~OpenRouter() = default;
|
||||||
|
|
||||||
int OpenRouter::serve(const std::string &ip, int port, int socket_flags)
|
int OpenRouter::serve(const std::string& ip, int port, int socket_flags)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW
|
std::cout << GREEN << "Starting" << RESET << " server on [ " << YELLOW
|
||||||
<< "0.0.0.0" << RESET << ":" << CYAN << "43244" << RESET << " ]" << std::endl;
|
<< "0.0.0.0" << RESET << ":" << CYAN << "43244" << RESET << " ]" << std::endl;
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
OpenRouter();
|
OpenRouter();
|
||||||
~OpenRouter();
|
~OpenRouter();
|
||||||
int serve(const std::string &ip, int port, int socket_flags = 0);
|
int serve(const std::string& ip, int port, int socket_flags = 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // OPENROUTER_HPP
|
#endif // OPENROUTER_HPP
|
||||||
+1
-1
@@ -4,7 +4,7 @@
|
|||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
#include <json.hpp>
|
#include <json.hpp>
|
||||||
|
|
||||||
static bool check_body(const httplib::Request &request)
|
static bool check_body(const httplib::Request& request)
|
||||||
{
|
{
|
||||||
if (request.body.empty())
|
if (request.body.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -37,46 +37,52 @@ using std::size_t;
|
|||||||
using std::vector;
|
using std::vector;
|
||||||
|
|
||||||
|
|
||||||
namespace qrcodegen {
|
namespace qrcodegen
|
||||||
|
{
|
||||||
|
|
||||||
/*---- Class QrSegment ----*/
|
/*---- Class QrSegment ----*/
|
||||||
|
|
||||||
QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
|
QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
|
||||||
modeBits(mode) {
|
modeBits(mode)
|
||||||
|
{
|
||||||
numBitsCharCount[0] = cc0;
|
numBitsCharCount[0] = cc0;
|
||||||
numBitsCharCount[1] = cc1;
|
numBitsCharCount[1] = cc1;
|
||||||
numBitsCharCount[2] = cc2;
|
numBitsCharCount[2] = cc2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrSegment::Mode::getModeBits() const {
|
int QrSegment::Mode::getModeBits() const
|
||||||
|
{
|
||||||
return modeBits;
|
return modeBits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrSegment::Mode::numCharCountBits(int ver) const {
|
int QrSegment::Mode::numCharCountBits(int ver) const
|
||||||
|
{
|
||||||
return numBitsCharCount[(ver + 7) / 17];
|
return numBitsCharCount[(ver + 7) / 17];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const QrSegment::Mode QrSegment::Mode::NUMERIC (0x1, 10, 12, 14);
|
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::ALPHANUMERIC(0x2, 9, 11, 13);
|
||||||
const QrSegment::Mode QrSegment::Mode::BYTE (0x4, 8, 16, 16);
|
const QrSegment::Mode QrSegment::Mode::BYTE(0x4, 8, 16, 16);
|
||||||
const QrSegment::Mode QrSegment::Mode::KANJI (0x8, 8, 10, 12);
|
const QrSegment::Mode QrSegment::Mode::KANJI(0x8, 8, 10, 12);
|
||||||
const QrSegment::Mode QrSegment::Mode::ECI (0x7, 0, 0, 0);
|
const QrSegment::Mode QrSegment::Mode::ECI(0x7, 0, 0, 0);
|
||||||
|
|
||||||
|
|
||||||
QrSegment QrSegment::makeBytes(const vector<uint8_t> &data) {
|
QrSegment QrSegment::makeBytes(const vector<uint8_t>& data)
|
||||||
|
{
|
||||||
if (data.size() > static_cast<unsigned int>(INT_MAX))
|
if (data.size() > static_cast<unsigned int>(INT_MAX))
|
||||||
throw std::length_error("Data too long");
|
throw std::length_error("Data too long");
|
||||||
BitBuffer bb;
|
BitBuffer bb;
|
||||||
for (uint8_t b : data)
|
for (uint8_t b : data)
|
||||||
bb.appendBits(b, 8);
|
bb.appendBits(b, 8);
|
||||||
return QrSegment(Mode::BYTE, static_cast<int>(data.size()), std::move(bb));
|
return QrSegment(Mode::BYTE, static_cast<int>(data.size()), std::move(bb));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrSegment QrSegment::makeNumeric(const char *digits) {
|
QrSegment QrSegment::makeNumeric(const char* digits)
|
||||||
|
{
|
||||||
BitBuffer bb;
|
BitBuffer bb;
|
||||||
int accumData = 0;
|
int accumData = 0;
|
||||||
int accumCount = 0;
|
int accumCount = 0;
|
||||||
@@ -96,16 +102,17 @@ QrSegment QrSegment::makeNumeric(const char *digits) {
|
|||||||
if (accumCount > 0) // 1 or 2 digits remaining
|
if (accumCount > 0) // 1 or 2 digits remaining
|
||||||
bb.appendBits(static_cast<uint32_t>(accumData), accumCount * 3 + 1);
|
bb.appendBits(static_cast<uint32_t>(accumData), accumCount * 3 + 1);
|
||||||
return QrSegment(Mode::NUMERIC, charCount, std::move(bb));
|
return QrSegment(Mode::NUMERIC, charCount, std::move(bb));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrSegment QrSegment::makeAlphanumeric(const char *text) {
|
QrSegment QrSegment::makeAlphanumeric(const char* text)
|
||||||
|
{
|
||||||
BitBuffer bb;
|
BitBuffer bb;
|
||||||
int accumData = 0;
|
int accumData = 0;
|
||||||
int accumCount = 0;
|
int accumCount = 0;
|
||||||
int charCount = 0;
|
int charCount = 0;
|
||||||
for (; *text != '\0'; text++, charCount++) {
|
for (; *text != '\0'; text++, charCount++) {
|
||||||
const char *temp = std::strchr(ALPHANUMERIC_CHARSET, *text);
|
const char* temp = std::strchr(ALPHANUMERIC_CHARSET, *text);
|
||||||
if (temp == nullptr)
|
if (temp == nullptr)
|
||||||
throw std::domain_error("String contains unencodable characters in alphanumeric mode");
|
throw std::domain_error("String contains unencodable characters in alphanumeric mode");
|
||||||
accumData = accumData * 45 + static_cast<int>(temp - ALPHANUMERIC_CHARSET);
|
accumData = accumData * 45 + static_cast<int>(temp - ALPHANUMERIC_CHARSET);
|
||||||
@@ -119,10 +126,11 @@ QrSegment QrSegment::makeAlphanumeric(const char *text) {
|
|||||||
if (accumCount > 0) // 1 character remaining
|
if (accumCount > 0) // 1 character remaining
|
||||||
bb.appendBits(static_cast<uint32_t>(accumData), 6);
|
bb.appendBits(static_cast<uint32_t>(accumData), 6);
|
||||||
return QrSegment(Mode::ALPHANUMERIC, charCount, std::move(bb));
|
return QrSegment(Mode::ALPHANUMERIC, charCount, std::move(bb));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
vector<QrSegment> QrSegment::makeSegments(const char *text) {
|
vector<QrSegment> QrSegment::makeSegments(const char* text)
|
||||||
|
{
|
||||||
// Select the most efficient segment encoding automatically
|
// Select the most efficient segment encoding automatically
|
||||||
vector<QrSegment> result;
|
vector<QrSegment> result;
|
||||||
if (*text == '\0'); // Leave result empty
|
if (*text == '\0'); // Leave result empty
|
||||||
@@ -137,10 +145,11 @@ vector<QrSegment> QrSegment::makeSegments(const char *text) {
|
|||||||
result.push_back(makeBytes(bytes));
|
result.push_back(makeBytes(bytes));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrSegment QrSegment::makeEci(long assignVal) {
|
QrSegment QrSegment::makeEci(long assignVal)
|
||||||
|
{
|
||||||
BitBuffer bb;
|
BitBuffer bb;
|
||||||
if (assignVal < 0)
|
if (assignVal < 0)
|
||||||
throw std::domain_error("ECI assignment value out of range");
|
throw std::domain_error("ECI assignment value out of range");
|
||||||
@@ -149,36 +158,41 @@ QrSegment QrSegment::makeEci(long assignVal) {
|
|||||||
else if (assignVal < (1 << 14)) {
|
else if (assignVal < (1 << 14)) {
|
||||||
bb.appendBits(2, 2);
|
bb.appendBits(2, 2);
|
||||||
bb.appendBits(static_cast<uint32_t>(assignVal), 14);
|
bb.appendBits(static_cast<uint32_t>(assignVal), 14);
|
||||||
} else if (assignVal < 1000000L) {
|
}
|
||||||
|
else if (assignVal < 1000000L) {
|
||||||
bb.appendBits(6, 3);
|
bb.appendBits(6, 3);
|
||||||
bb.appendBits(static_cast<uint32_t>(assignVal), 21);
|
bb.appendBits(static_cast<uint32_t>(assignVal), 21);
|
||||||
} else
|
}
|
||||||
|
else
|
||||||
throw std::domain_error("ECI assignment value out of range");
|
throw std::domain_error("ECI assignment value out of range");
|
||||||
return QrSegment(Mode::ECI, 0, std::move(bb));
|
return QrSegment(Mode::ECI, 0, std::move(bb));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt) :
|
QrSegment::QrSegment(const Mode& md, int numCh, const std::vector<bool>& dt) :
|
||||||
mode(&md),
|
mode(&md),
|
||||||
numChars(numCh),
|
numChars(numCh),
|
||||||
data(dt) {
|
data(dt)
|
||||||
|
{
|
||||||
if (numCh < 0)
|
if (numCh < 0)
|
||||||
throw std::domain_error("Invalid value");
|
throw std::domain_error("Invalid value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrSegment::QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt) :
|
QrSegment::QrSegment(const Mode& md, int numCh, std::vector<bool>&& dt) :
|
||||||
mode(&md),
|
mode(&md),
|
||||||
numChars(numCh),
|
numChars(numCh),
|
||||||
data(std::move(dt)) {
|
data(std::move(dt))
|
||||||
|
{
|
||||||
if (numCh < 0)
|
if (numCh < 0)
|
||||||
throw std::domain_error("Invalid value");
|
throw std::domain_error("Invalid value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {
|
int QrSegment::getTotalBits(const vector<QrSegment>& segs, int version)
|
||||||
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
for (const QrSegment &seg : segs) {
|
for (const QrSegment& seg : segs) {
|
||||||
int ccbits = seg.mode->numCharCountBits(version);
|
int ccbits = seg.mode->numCharCountBits(version);
|
||||||
if (seg.numChars >= (1L << ccbits))
|
if (seg.numChars >= (1L << ccbits))
|
||||||
return -1; // The segment's length doesn't fit the field's bit width
|
return -1; // The segment's length doesn't fit the field's bit width
|
||||||
@@ -190,74 +204,83 @@ int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {
|
|||||||
result += static_cast<int>(seg.data.size());
|
result += static_cast<int>(seg.data.size());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool QrSegment::isNumeric(const char *text) {
|
bool QrSegment::isNumeric(const char* text)
|
||||||
|
{
|
||||||
for (; *text != '\0'; text++) {
|
for (; *text != '\0'; text++) {
|
||||||
char c = *text;
|
char c = *text;
|
||||||
if (c < '0' || c > '9')
|
if (c < '0' || c > '9')
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool QrSegment::isAlphanumeric(const char *text) {
|
bool QrSegment::isAlphanumeric(const char* text)
|
||||||
|
{
|
||||||
for (; *text != '\0'; text++) {
|
for (; *text != '\0'; text++) {
|
||||||
if (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr)
|
if (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const QrSegment::Mode &QrSegment::getMode() const {
|
const QrSegment::Mode& QrSegment::getMode() const
|
||||||
|
{
|
||||||
return *mode;
|
return *mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrSegment::getNumChars() const {
|
int QrSegment::getNumChars() const
|
||||||
|
{
|
||||||
return numChars;
|
return numChars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const std::vector<bool> &QrSegment::getData() const {
|
const std::vector<bool>& QrSegment::getData() const
|
||||||
|
{
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const char *QrSegment::ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
const char* QrSegment::ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Class QrCode ----*/
|
/*---- Class QrCode ----*/
|
||||||
|
|
||||||
int QrCode::getFormatBits(Ecc ecl) {
|
int QrCode::getFormatBits(Ecc ecl)
|
||||||
|
{
|
||||||
switch (ecl) {
|
switch (ecl) {
|
||||||
case Ecc::LOW : return 1;
|
case Ecc::LOW: return 1;
|
||||||
case Ecc::MEDIUM : return 0;
|
case Ecc::MEDIUM: return 0;
|
||||||
case Ecc::QUARTILE: return 3;
|
case Ecc::QUARTILE: return 3;
|
||||||
case Ecc::HIGH : return 2;
|
case Ecc::HIGH: return 2;
|
||||||
default: throw std::logic_error("Unreachable");
|
default: throw std::logic_error("Unreachable");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrCode QrCode::encodeText(const char *text, Ecc ecl) {
|
QrCode QrCode::encodeText(const char* text, Ecc ecl)
|
||||||
|
{
|
||||||
vector<QrSegment> segs = QrSegment::makeSegments(text);
|
vector<QrSegment> segs = QrSegment::makeSegments(text);
|
||||||
return encodeSegments(segs, ecl);
|
return encodeSegments(segs, ecl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrCode QrCode::encodeBinary(const vector<uint8_t> &data, Ecc ecl) {
|
QrCode QrCode::encodeBinary(const vector<uint8_t>& data, Ecc ecl)
|
||||||
vector<QrSegment> segs{QrSegment::makeBytes(data)};
|
{
|
||||||
|
vector<QrSegment> segs{ QrSegment::makeBytes(data) };
|
||||||
return encodeSegments(segs, ecl);
|
return encodeSegments(segs, ecl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,
|
QrCode QrCode::encodeSegments(const vector<QrSegment>& segs, Ecc ecl,
|
||||||
int minVersion, int maxVersion, int mask, bool boostEcl) {
|
int minVersion, int maxVersion, int mask, bool boostEcl)
|
||||||
|
{
|
||||||
if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)
|
if (!(MIN_VERSION <= minVersion && minVersion <= maxVersion && maxVersion <= MAX_VERSION) || mask < -1 || mask > 7)
|
||||||
throw std::invalid_argument("Invalid value");
|
throw std::invalid_argument("Invalid value");
|
||||||
|
|
||||||
@@ -289,7 +312,7 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,
|
|||||||
|
|
||||||
// Concatenate all segments to create the data bit string
|
// Concatenate all segments to create the data bit string
|
||||||
BitBuffer bb;
|
BitBuffer bb;
|
||||||
for (const QrSegment &seg : segs) {
|
for (const QrSegment& seg : segs) {
|
||||||
bb.appendBits(static_cast<uint32_t>(seg.getMode().getModeBits()), 4);
|
bb.appendBits(static_cast<uint32_t>(seg.getMode().getModeBits()), 4);
|
||||||
bb.appendBits(static_cast<uint32_t>(seg.getNumChars()), seg.getMode().numCharCountBits(version));
|
bb.appendBits(static_cast<uint32_t>(seg.getNumChars()), seg.getMode().numCharCountBits(version));
|
||||||
bb.insert(bb.end(), seg.getData().begin(), seg.getData().end());
|
bb.insert(bb.end(), seg.getData().begin(), seg.getData().end());
|
||||||
@@ -314,13 +337,14 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,
|
|||||||
|
|
||||||
// Create the QR Code object
|
// Create the QR Code object
|
||||||
return QrCode(version, ecl, dataCodewords, mask);
|
return QrCode(version, ecl, dataCodewords, mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int msk) :
|
QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t>& dataCodewords, int msk) :
|
||||||
// Initialize fields and check arguments
|
// Initialize fields and check arguments
|
||||||
version(ver),
|
version(ver),
|
||||||
errorCorrectionLevel(ecl) {
|
errorCorrectionLevel(ecl)
|
||||||
|
{
|
||||||
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
||||||
throw std::domain_error("Version value out of range");
|
throw std::domain_error("Version value out of range");
|
||||||
if (msk < -1 || msk > 7)
|
if (msk < -1 || msk > 7)
|
||||||
@@ -356,35 +380,41 @@ QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int msk)
|
|||||||
|
|
||||||
isFunction.clear();
|
isFunction.clear();
|
||||||
isFunction.shrink_to_fit();
|
isFunction.shrink_to_fit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrCode::getVersion() const {
|
int QrCode::getVersion() const
|
||||||
|
{
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrCode::getSize() const {
|
int QrCode::getSize() const
|
||||||
|
{
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QrCode::Ecc QrCode::getErrorCorrectionLevel() const {
|
QrCode::Ecc QrCode::getErrorCorrectionLevel() const
|
||||||
|
{
|
||||||
return errorCorrectionLevel;
|
return errorCorrectionLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrCode::getMask() const {
|
int QrCode::getMask() const
|
||||||
|
{
|
||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool QrCode::getModule(int x, int y) const {
|
bool QrCode::getModule(int x, int y) const
|
||||||
|
{
|
||||||
return 0 <= x && x < size && 0 <= y && y < size && module(x, y);
|
return 0 <= x && x < size && 0 <= y && y < size && module(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::drawFunctionPatterns() {
|
void QrCode::drawFunctionPatterns()
|
||||||
|
{
|
||||||
// Draw horizontal and vertical timing patterns
|
// Draw horizontal and vertical timing patterns
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
setFunctionModule(6, i, i % 2 == 0);
|
setFunctionModule(6, i, i % 2 == 0);
|
||||||
@@ -410,10 +440,11 @@ void QrCode::drawFunctionPatterns() {
|
|||||||
// Draw configuration data
|
// Draw configuration data
|
||||||
drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
|
drawFormatBits(0); // Dummy mask value; overwritten later in the constructor
|
||||||
drawVersion();
|
drawVersion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::drawFormatBits(int msk) {
|
void QrCode::drawFormatBits(int msk)
|
||||||
|
{
|
||||||
// Calculate error correction code and pack bits
|
// Calculate error correction code and pack bits
|
||||||
int data = getFormatBits(errorCorrectionLevel) << 3 | msk; // errCorrLvl is uint2, msk is uint3
|
int data = getFormatBits(errorCorrectionLevel) << 3 | msk; // errCorrLvl is uint2, msk is uint3
|
||||||
int rem = data;
|
int rem = data;
|
||||||
@@ -437,10 +468,11 @@ void QrCode::drawFormatBits(int msk) {
|
|||||||
for (int i = 8; i < 15; i++)
|
for (int i = 8; i < 15; i++)
|
||||||
setFunctionModule(8, size - 15 + i, getBit(bits, i));
|
setFunctionModule(8, size - 15 + i, getBit(bits, i));
|
||||||
setFunctionModule(8, size - 8, true); // Always dark
|
setFunctionModule(8, size - 8, true); // Always dark
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::drawVersion() {
|
void QrCode::drawVersion()
|
||||||
|
{
|
||||||
if (version < 7)
|
if (version < 7)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -459,10 +491,11 @@ void QrCode::drawVersion() {
|
|||||||
setFunctionModule(a, b, bit);
|
setFunctionModule(a, b, bit);
|
||||||
setFunctionModule(b, a, bit);
|
setFunctionModule(b, a, bit);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::drawFinderPattern(int x, int y) {
|
void QrCode::drawFinderPattern(int x, int y)
|
||||||
|
{
|
||||||
for (int dy = -4; dy <= 4; dy++) {
|
for (int dy = -4; dy <= 4; dy++) {
|
||||||
for (int dx = -4; dx <= 4; dx++) {
|
for (int dx = -4; dx <= 4; dx++) {
|
||||||
int dist = std::max(std::abs(dx), std::abs(dy)); // Chebyshev/infinity norm
|
int dist = std::max(std::abs(dx), std::abs(dy)); // Chebyshev/infinity norm
|
||||||
@@ -471,37 +504,41 @@ void QrCode::drawFinderPattern(int x, int y) {
|
|||||||
setFunctionModule(xx, yy, dist != 2 && dist != 4);
|
setFunctionModule(xx, yy, dist != 2 && dist != 4);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::drawAlignmentPattern(int x, int y) {
|
void QrCode::drawAlignmentPattern(int x, int y)
|
||||||
|
{
|
||||||
for (int dy = -2; dy <= 2; dy++) {
|
for (int dy = -2; dy <= 2; dy++) {
|
||||||
for (int dx = -2; dx <= 2; dx++)
|
for (int dx = -2; dx <= 2; dx++)
|
||||||
setFunctionModule(x + dx, y + dy, std::max(std::abs(dx), std::abs(dy)) != 1);
|
setFunctionModule(x + dx, y + dy, std::max(std::abs(dx), std::abs(dy)) != 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::setFunctionModule(int x, int y, bool isDark) {
|
void QrCode::setFunctionModule(int x, int y, bool isDark)
|
||||||
|
{
|
||||||
size_t ux = static_cast<size_t>(x);
|
size_t ux = static_cast<size_t>(x);
|
||||||
size_t uy = static_cast<size_t>(y);
|
size_t uy = static_cast<size_t>(y);
|
||||||
modules .at(uy).at(ux) = isDark;
|
modules.at(uy).at(ux) = isDark;
|
||||||
isFunction.at(uy).at(ux) = true;
|
isFunction.at(uy).at(ux) = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool QrCode::module(int x, int y) const {
|
bool QrCode::module(int x, int y) const
|
||||||
|
{
|
||||||
return modules.at(static_cast<size_t>(y)).at(static_cast<size_t>(x));
|
return modules.at(static_cast<size_t>(y)).at(static_cast<size_t>(x));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
vector<uint8_t> QrCode::addEccAndInterleave(const vector<uint8_t> &data) const {
|
vector<uint8_t> QrCode::addEccAndInterleave(const vector<uint8_t>& data) const
|
||||||
|
{
|
||||||
if (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))
|
if (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))
|
||||||
throw std::invalid_argument("Invalid argument");
|
throw std::invalid_argument("Invalid argument");
|
||||||
|
|
||||||
// Calculate parameter numbers
|
// Calculate parameter numbers
|
||||||
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(errorCorrectionLevel)][version];
|
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(errorCorrectionLevel)][version];
|
||||||
int blockEccLen = ECC_CODEWORDS_PER_BLOCK [static_cast<int>(errorCorrectionLevel)][version];
|
int blockEccLen = ECC_CODEWORDS_PER_BLOCK[static_cast<int>(errorCorrectionLevel)][version];
|
||||||
int rawCodewords = getNumRawDataModules(version) / 8;
|
int rawCodewords = getNumRawDataModules(version) / 8;
|
||||||
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
int numShortBlocks = numBlocks - rawCodewords % numBlocks;
|
||||||
int shortBlockLen = rawCodewords / numBlocks;
|
int shortBlockLen = rawCodewords / numBlocks;
|
||||||
@@ -530,10 +567,11 @@ vector<uint8_t> QrCode::addEccAndInterleave(const vector<uint8_t> &data) const {
|
|||||||
}
|
}
|
||||||
assert(result.size() == static_cast<unsigned int>(rawCodewords));
|
assert(result.size() == static_cast<unsigned int>(rawCodewords));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::drawCodewords(const vector<uint8_t> &data) {
|
void QrCode::drawCodewords(const vector<uint8_t>& data)
|
||||||
|
{
|
||||||
if (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))
|
if (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))
|
||||||
throw std::invalid_argument("Invalid argument");
|
throw std::invalid_argument("Invalid argument");
|
||||||
|
|
||||||
@@ -557,10 +595,11 @@ void QrCode::drawCodewords(const vector<uint8_t> &data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert(i == data.size() * 8);
|
assert(i == data.size() * 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::applyMask(int msk) {
|
void QrCode::applyMask(int msk)
|
||||||
|
{
|
||||||
if (msk < 0 || msk > 7)
|
if (msk < 0 || msk > 7)
|
||||||
throw std::domain_error("Mask value out of range");
|
throw std::domain_error("Mask value out of range");
|
||||||
size_t sz = static_cast<size_t>(size);
|
size_t sz = static_cast<size_t>(size);
|
||||||
@@ -581,17 +620,18 @@ void QrCode::applyMask(int msk) {
|
|||||||
modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));
|
modules.at(y).at(x) = modules.at(y).at(x) ^ (invert & !isFunction.at(y).at(x));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
long QrCode::getPenaltyScore() const {
|
long QrCode::getPenaltyScore() const
|
||||||
|
{
|
||||||
long result = 0;
|
long result = 0;
|
||||||
|
|
||||||
// Adjacent modules in row having same color, and finder-like patterns
|
// Adjacent modules in row having same color, and finder-like patterns
|
||||||
for (int y = 0; y < size; y++) {
|
for (int y = 0; y < size; y++) {
|
||||||
bool runColor = false;
|
bool runColor = false;
|
||||||
int runX = 0;
|
int runX = 0;
|
||||||
std::array<int,7> runHistory = {};
|
std::array<int, 7> runHistory = {};
|
||||||
for (int x = 0; x < size; x++) {
|
for (int x = 0; x < size; x++) {
|
||||||
if (module(x, y) == runColor) {
|
if (module(x, y) == runColor) {
|
||||||
runX++;
|
runX++;
|
||||||
@@ -599,7 +639,8 @@ long QrCode::getPenaltyScore() const {
|
|||||||
result += PENALTY_N1;
|
result += PENALTY_N1;
|
||||||
else if (runX > 5)
|
else if (runX > 5)
|
||||||
result++;
|
result++;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
finderPenaltyAddHistory(runX, runHistory);
|
finderPenaltyAddHistory(runX, runHistory);
|
||||||
if (!runColor)
|
if (!runColor)
|
||||||
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
||||||
@@ -613,7 +654,7 @@ long QrCode::getPenaltyScore() const {
|
|||||||
for (int x = 0; x < size; x++) {
|
for (int x = 0; x < size; x++) {
|
||||||
bool runColor = false;
|
bool runColor = false;
|
||||||
int runY = 0;
|
int runY = 0;
|
||||||
std::array<int,7> runHistory = {};
|
std::array<int, 7> runHistory = {};
|
||||||
for (int y = 0; y < size; y++) {
|
for (int y = 0; y < size; y++) {
|
||||||
if (module(x, y) == runColor) {
|
if (module(x, y) == runColor) {
|
||||||
runY++;
|
runY++;
|
||||||
@@ -621,7 +662,8 @@ long QrCode::getPenaltyScore() const {
|
|||||||
result += PENALTY_N1;
|
result += PENALTY_N1;
|
||||||
else if (runY > 5)
|
else if (runY > 5)
|
||||||
result++;
|
result++;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
finderPenaltyAddHistory(runY, runHistory);
|
finderPenaltyAddHistory(runY, runHistory);
|
||||||
if (!runColor)
|
if (!runColor)
|
||||||
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
result += finderPenaltyCountPatterns(runHistory) * PENALTY_N3;
|
||||||
@@ -636,7 +678,7 @@ long QrCode::getPenaltyScore() const {
|
|||||||
for (int y = 0; y < size - 1; y++) {
|
for (int y = 0; y < size - 1; y++) {
|
||||||
for (int x = 0; x < size - 1; x++) {
|
for (int x = 0; x < size - 1; x++) {
|
||||||
bool color = module(x, y);
|
bool color = module(x, y);
|
||||||
if ( color == module(x + 1, y) &&
|
if (color == module(x + 1, y) &&
|
||||||
color == module(x, y + 1) &&
|
color == module(x, y + 1) &&
|
||||||
color == module(x + 1, y + 1))
|
color == module(x + 1, y + 1))
|
||||||
result += PENALTY_N2;
|
result += PENALTY_N2;
|
||||||
@@ -645,7 +687,7 @@ long QrCode::getPenaltyScore() const {
|
|||||||
|
|
||||||
// Balance of dark and light modules
|
// Balance of dark and light modules
|
||||||
int dark = 0;
|
int dark = 0;
|
||||||
for (const vector<bool> &row : modules) {
|
for (const vector<bool>& row : modules) {
|
||||||
for (bool color : row) {
|
for (bool color : row) {
|
||||||
if (color)
|
if (color)
|
||||||
dark++;
|
dark++;
|
||||||
@@ -658,10 +700,11 @@ long QrCode::getPenaltyScore() const {
|
|||||||
result += k * PENALTY_N4;
|
result += k * PENALTY_N4;
|
||||||
assert(0 <= result && result <= 2568888L); // Non-tight upper bound based on default values of PENALTY_N1, ..., N4
|
assert(0 <= result && result <= 2568888L); // Non-tight upper bound based on default values of PENALTY_N1, ..., N4
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
vector<int> QrCode::getAlignmentPatternPositions() const {
|
vector<int> QrCode::getAlignmentPatternPositions() const
|
||||||
|
{
|
||||||
if (version == 1)
|
if (version == 1)
|
||||||
return vector<int>();
|
return vector<int>();
|
||||||
else {
|
else {
|
||||||
@@ -674,10 +717,11 @@ vector<int> QrCode::getAlignmentPatternPositions() const {
|
|||||||
result.insert(result.begin(), 6);
|
result.insert(result.begin(), 6);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrCode::getNumRawDataModules(int ver) {
|
int QrCode::getNumRawDataModules(int ver)
|
||||||
|
{
|
||||||
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
if (ver < MIN_VERSION || ver > MAX_VERSION)
|
||||||
throw std::domain_error("Version number out of range");
|
throw std::domain_error("Version number out of range");
|
||||||
int result = (16 * ver + 128) * ver + 64;
|
int result = (16 * ver + 128) * ver + 64;
|
||||||
@@ -689,17 +733,19 @@ int QrCode::getNumRawDataModules(int ver) {
|
|||||||
}
|
}
|
||||||
assert(208 <= result && result <= 29648);
|
assert(208 <= result && result <= 29648);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrCode::getNumDataCodewords(int ver, Ecc ecl) {
|
int QrCode::getNumDataCodewords(int ver, Ecc ecl)
|
||||||
|
{
|
||||||
return getNumRawDataModules(ver) / 8
|
return getNumRawDataModules(ver) / 8
|
||||||
- ECC_CODEWORDS_PER_BLOCK [static_cast<int>(ecl)][ver]
|
- ECC_CODEWORDS_PER_BLOCK[static_cast<int>(ecl)][ver]
|
||||||
* NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver];
|
* NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
vector<uint8_t> QrCode::reedSolomonComputeDivisor(int degree) {
|
vector<uint8_t> QrCode::reedSolomonComputeDivisor(int degree)
|
||||||
|
{
|
||||||
if (degree < 1 || degree > 255)
|
if (degree < 1 || degree > 255)
|
||||||
throw std::domain_error("Degree out of range");
|
throw std::domain_error("Degree out of range");
|
||||||
// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
|
// Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
|
||||||
@@ -721,10 +767,11 @@ vector<uint8_t> QrCode::reedSolomonComputeDivisor(int degree) {
|
|||||||
root = reedSolomonMultiply(root, 0x02);
|
root = reedSolomonMultiply(root, 0x02);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
vector<uint8_t> QrCode::reedSolomonComputeRemainder(const vector<uint8_t> &data, const vector<uint8_t> &divisor) {
|
vector<uint8_t> QrCode::reedSolomonComputeRemainder(const vector<uint8_t>& data, const vector<uint8_t>& divisor)
|
||||||
|
{
|
||||||
vector<uint8_t> result(divisor.size());
|
vector<uint8_t> result(divisor.size());
|
||||||
for (uint8_t b : data) { // Polynomial division
|
for (uint8_t b : data) { // Polynomial division
|
||||||
uint8_t factor = b ^ result.at(0);
|
uint8_t factor = b ^ result.at(0);
|
||||||
@@ -734,10 +781,11 @@ vector<uint8_t> QrCode::reedSolomonComputeRemainder(const vector<uint8_t> &data,
|
|||||||
result.at(i) ^= reedSolomonMultiply(divisor.at(i), factor);
|
result.at(i) ^= reedSolomonMultiply(divisor.at(i), factor);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint8_t QrCode::reedSolomonMultiply(uint8_t x, uint8_t y) {
|
uint8_t QrCode::reedSolomonMultiply(uint8_t x, uint8_t y)
|
||||||
|
{
|
||||||
// Russian peasant multiplication
|
// Russian peasant multiplication
|
||||||
int z = 0;
|
int z = 0;
|
||||||
for (int i = 7; i >= 0; i--) {
|
for (int i = 7; i >= 0; i--) {
|
||||||
@@ -746,19 +794,21 @@ uint8_t QrCode::reedSolomonMultiply(uint8_t x, uint8_t y) {
|
|||||||
}
|
}
|
||||||
assert(z >> 8 == 0);
|
assert(z >> 8 == 0);
|
||||||
return static_cast<uint8_t>(z);
|
return static_cast<uint8_t>(z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrCode::finderPenaltyCountPatterns(const std::array<int,7> &runHistory) const {
|
int QrCode::finderPenaltyCountPatterns(const std::array<int, 7>& runHistory) const
|
||||||
|
{
|
||||||
int n = runHistory.at(1);
|
int n = runHistory.at(1);
|
||||||
assert(n <= size * 3);
|
assert(n <= size * 3);
|
||||||
bool core = n > 0 && runHistory.at(2) == n && runHistory.at(3) == n * 3 && runHistory.at(4) == n && runHistory.at(5) == n;
|
bool core = n > 0 && runHistory.at(2) == n && runHistory.at(3) == n * 3 && runHistory.at(4) == n && runHistory.at(5) == n;
|
||||||
return (core && runHistory.at(0) >= n * 4 && runHistory.at(6) >= n ? 1 : 0)
|
return (core && runHistory.at(0) >= n * 4 && runHistory.at(6) >= n ? 1 : 0)
|
||||||
+ (core && runHistory.at(6) >= n * 4 && runHistory.at(0) >= n ? 1 : 0);
|
+ (core && runHistory.at(6) >= n * 4 && runHistory.at(0) >= n ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int QrCode::finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int,7> &runHistory) const {
|
int QrCode::finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int, 7>& runHistory) const
|
||||||
|
{
|
||||||
if (currentRunColor) { // Terminate dark run
|
if (currentRunColor) { // Terminate dark run
|
||||||
finderPenaltyAddHistory(currentRunLength, runHistory);
|
finderPenaltyAddHistory(currentRunLength, runHistory);
|
||||||
currentRunLength = 0;
|
currentRunLength = 0;
|
||||||
@@ -766,65 +816,68 @@ int QrCode::finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunL
|
|||||||
currentRunLength += size; // Add light border to final run
|
currentRunLength += size; // Add light border to final run
|
||||||
finderPenaltyAddHistory(currentRunLength, runHistory);
|
finderPenaltyAddHistory(currentRunLength, runHistory);
|
||||||
return finderPenaltyCountPatterns(runHistory);
|
return finderPenaltyCountPatterns(runHistory);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QrCode::finderPenaltyAddHistory(int currentRunLength, std::array<int,7> &runHistory) const {
|
void QrCode::finderPenaltyAddHistory(int currentRunLength, std::array<int, 7>& runHistory) const
|
||||||
|
{
|
||||||
if (runHistory.at(0) == 0)
|
if (runHistory.at(0) == 0)
|
||||||
currentRunLength += size; // Add light border to initial run
|
currentRunLength += size; // Add light border to initial run
|
||||||
std::copy_backward(runHistory.cbegin(), runHistory.cend() - 1, runHistory.end());
|
std::copy_backward(runHistory.cbegin(), runHistory.cend() - 1, runHistory.end());
|
||||||
runHistory.at(0) = currentRunLength;
|
runHistory.at(0) = currentRunLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool QrCode::getBit(long x, int i) {
|
bool QrCode::getBit(long x, int i)
|
||||||
|
{
|
||||||
return ((x >> i) & 1) != 0;
|
return ((x >> i) & 1) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*---- Tables of constants ----*/
|
/*---- Tables of constants ----*/
|
||||||
|
|
||||||
const int QrCode::PENALTY_N1 = 3;
|
const int QrCode::PENALTY_N1 = 3;
|
||||||
const int QrCode::PENALTY_N2 = 3;
|
const int QrCode::PENALTY_N2 = 3;
|
||||||
const int QrCode::PENALTY_N3 = 40;
|
const int QrCode::PENALTY_N3 = 40;
|
||||||
const int QrCode::PENALTY_N4 = 10;
|
const int QrCode::PENALTY_N4 = 10;
|
||||||
|
|
||||||
|
|
||||||
const int8_t QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {
|
const int8_t QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {
|
||||||
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
||||||
//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
|
//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
|
||||||
{-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
|
{-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
|
||||||
{-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
|
{-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
|
||||||
{-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
|
{-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
|
||||||
{-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
|
{-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
|
||||||
};
|
};
|
||||||
|
|
||||||
const int8_t QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
|
const int8_t QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
|
||||||
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
// Version: (note that index 0 is for padding, and is set to an illegal value)
|
||||||
//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
|
//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
|
||||||
{-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
|
{-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
|
||||||
{-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
|
{-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
|
||||||
{-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
|
{-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
|
||||||
{-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
|
{-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
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
data_too_long::data_too_long(const std::string &msg) :
|
data_too_long::data_too_long(const std::string& msg) :
|
||||||
std::length_error(msg) {}
|
std::length_error(msg) {}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Class BitBuffer ----*/
|
/*---- Class BitBuffer ----*/
|
||||||
|
|
||||||
BitBuffer::BitBuffer()
|
BitBuffer::BitBuffer()
|
||||||
: std::vector<bool>() {}
|
: std::vector<bool>() {}
|
||||||
|
|
||||||
|
|
||||||
void BitBuffer::appendBits(std::uint32_t val, int len) {
|
void BitBuffer::appendBits(std::uint32_t val, int len)
|
||||||
|
{
|
||||||
if (len < 0 || len > 31 || val >> len != 0)
|
if (len < 0 || len > 31 || val >> len != 0)
|
||||||
throw std::domain_error("Value out of range");
|
throw std::domain_error("Value out of range");
|
||||||
for (int i = len - 1; i >= 0; i--) // Append bit by bit
|
for (int i = len - 1; i >= 0; i--) // Append bit by bit
|
||||||
this->push_back(((val >> i) & 1) != 0);
|
this->push_back(((val >> i) & 1) != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -30,9 +30,10 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
namespace qrcodegen {
|
namespace qrcodegen
|
||||||
|
{
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A segment of character/binary/control data in a QR Code symbol.
|
* A segment of character/binary/control data in a QR Code symbol.
|
||||||
* Instances of this class are immutable.
|
* Instances of this class are immutable.
|
||||||
* The mid-level way to create a segment is to take the payload data
|
* The mid-level way to create a segment is to take the payload data
|
||||||
@@ -43,7 +44,7 @@ namespace qrcodegen {
|
|||||||
* Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
|
* Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
|
||||||
* Any segment longer than this is meaningless for the purpose of generating QR Codes.
|
* Any segment longer than this is meaningless for the purpose of generating QR Codes.
|
||||||
*/
|
*/
|
||||||
class QrSegment final {
|
class QrSegment final {
|
||||||
|
|
||||||
/*---- Public helper enumeration ----*/
|
/*---- Public helper enumeration ----*/
|
||||||
|
|
||||||
@@ -99,13 +100,13 @@ class QrSegment final {
|
|||||||
* byte mode. All input byte vectors are acceptable. Any text string
|
* byte mode. All input byte vectors are acceptable. Any text string
|
||||||
* can be converted to UTF-8 bytes and encoded as a byte mode segment.
|
* can be converted to UTF-8 bytes and encoded as a byte mode segment.
|
||||||
*/
|
*/
|
||||||
public: static QrSegment makeBytes(const std::vector<std::uint8_t> &data);
|
public: static QrSegment makeBytes(const std::vector<std::uint8_t>& data);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Returns a segment representing the given string of decimal digits encoded in numeric mode.
|
* Returns a segment representing the given string of decimal digits encoded in numeric mode.
|
||||||
*/
|
*/
|
||||||
public: static QrSegment makeNumeric(const char *digits);
|
public: static QrSegment makeNumeric(const char* digits);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -113,14 +114,14 @@ class QrSegment final {
|
|||||||
* The characters allowed are: 0 to 9, A to Z (uppercase only), space,
|
* The characters allowed are: 0 to 9, A to Z (uppercase only), space,
|
||||||
* dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
* dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
||||||
*/
|
*/
|
||||||
public: static QrSegment makeAlphanumeric(const char *text);
|
public: static QrSegment makeAlphanumeric(const char* text);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Returns a list of zero or more segments to represent the given text string. The result
|
* Returns a list of zero or more segments to represent the given text string. The result
|
||||||
* may use various segment modes and switch modes to optimize the length of the bit stream.
|
* may use various segment modes and switch modes to optimize the length of the bit stream.
|
||||||
*/
|
*/
|
||||||
public: static std::vector<QrSegment> makeSegments(const char *text);
|
public: static std::vector<QrSegment> makeSegments(const char* text);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -136,7 +137,7 @@ class QrSegment final {
|
|||||||
* Tests whether the given string can be encoded as a segment in numeric mode.
|
* Tests whether the given string can be encoded as a segment in numeric mode.
|
||||||
* A string is encodable iff each character is in the range 0 to 9.
|
* A string is encodable iff each character is in the range 0 to 9.
|
||||||
*/
|
*/
|
||||||
public: static bool isNumeric(const char *text);
|
public: static bool isNumeric(const char* text);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -144,14 +145,14 @@ class QrSegment final {
|
|||||||
* A string is encodable iff each character is in the following set: 0 to 9, A to Z
|
* A string is encodable iff each character is in the following set: 0 to 9, A to Z
|
||||||
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
* (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
||||||
*/
|
*/
|
||||||
public: static bool isAlphanumeric(const char *text);
|
public: static bool isAlphanumeric(const char* text);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Instance fields ----*/
|
/*---- Instance fields ----*/
|
||||||
|
|
||||||
/* The mode indicator of this segment. Accessed through getMode(). */
|
/* The mode indicator of this segment. Accessed through getMode(). */
|
||||||
private: const Mode *mode;
|
private: const Mode* mode;
|
||||||
|
|
||||||
/* The length of this segment's unencoded data. Measured in characters for
|
/* The length of this segment's unencoded data. Measured in characters for
|
||||||
* numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
|
* numeric/alphanumeric/kanji mode, bytes for byte mode, and 0 for ECI mode.
|
||||||
@@ -170,7 +171,7 @@ class QrSegment final {
|
|||||||
* The character count (numCh) must agree with the mode and the bit buffer length,
|
* The character count (numCh) must agree with the mode and the bit buffer length,
|
||||||
* but the constraint isn't checked. The given bit buffer is copied and stored.
|
* but the constraint isn't checked. The given bit buffer is copied and stored.
|
||||||
*/
|
*/
|
||||||
public: QrSegment(const Mode &md, int numCh, const std::vector<bool> &dt);
|
public: QrSegment(const Mode& md, int numCh, const std::vector<bool>& dt);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -178,7 +179,7 @@ class QrSegment final {
|
|||||||
* The character count (numCh) must agree with the mode and the bit buffer length,
|
* The character count (numCh) must agree with the mode and the bit buffer length,
|
||||||
* but the constraint isn't checked. The given bit buffer is moved and stored.
|
* but the constraint isn't checked. The given bit buffer is moved and stored.
|
||||||
*/
|
*/
|
||||||
public: QrSegment(const Mode &md, int numCh, std::vector<bool> &&dt);
|
public: QrSegment(const Mode& md, int numCh, std::vector<bool>&& dt);
|
||||||
|
|
||||||
|
|
||||||
/*---- Methods ----*/
|
/*---- Methods ----*/
|
||||||
@@ -186,7 +187,7 @@ class QrSegment final {
|
|||||||
/*
|
/*
|
||||||
* Returns the mode field of this segment.
|
* Returns the mode field of this segment.
|
||||||
*/
|
*/
|
||||||
public: const Mode &getMode() const;
|
public: const Mode& getMode() const;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -198,26 +199,26 @@ class QrSegment final {
|
|||||||
/*
|
/*
|
||||||
* Returns the data bits of this segment.
|
* Returns the data bits of this segment.
|
||||||
*/
|
*/
|
||||||
public: const std::vector<bool> &getData() const;
|
public: const std::vector<bool>& getData() const;
|
||||||
|
|
||||||
|
|
||||||
// (Package-private) Calculates the number of bits needed to encode the given segments at
|
// (Package-private) Calculates the number of bits needed to encode the given segments at
|
||||||
// the given version. Returns a non-negative number if successful. Otherwise returns -1 if a
|
// the given version. Returns a non-negative number if successful. Otherwise returns -1 if a
|
||||||
// segment has too many characters to fit its length field, or the total bits exceeds INT_MAX.
|
// segment has too many characters to fit its length field, or the total bits exceeds INT_MAX.
|
||||||
public: static int getTotalBits(const std::vector<QrSegment> &segs, int version);
|
public: static int getTotalBits(const std::vector<QrSegment>& segs, int version);
|
||||||
|
|
||||||
|
|
||||||
/*---- Private constant ----*/
|
/*---- Private constant ----*/
|
||||||
|
|
||||||
/* The set of all legal characters in alphanumeric mode, where
|
/* The set of all legal characters in alphanumeric mode, where
|
||||||
* each character value maps to the index in the string. */
|
* each character value maps to the index in the string. */
|
||||||
private: static const char *ALPHANUMERIC_CHARSET;
|
private: static const char* ALPHANUMERIC_CHARSET;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A QR Code symbol, which is a type of two-dimension barcode.
|
* A QR Code symbol, which is a type of two-dimension barcode.
|
||||||
* Invented by Denso Wave and described in the ISO/IEC 18004 standard.
|
* Invented by Denso Wave and described in the ISO/IEC 18004 standard.
|
||||||
* Instances of this class represent an immutable square grid of dark and light cells.
|
* Instances of this class represent an immutable square grid of dark and light cells.
|
||||||
@@ -233,7 +234,7 @@ class QrSegment final {
|
|||||||
* supply the appropriate version number, and call the QrCode() constructor.
|
* supply the appropriate version number, and call the QrCode() constructor.
|
||||||
* (Note that all ways require supplying the desired error correction level.)
|
* (Note that all ways require supplying the desired error correction level.)
|
||||||
*/
|
*/
|
||||||
class QrCode final {
|
class QrCode final {
|
||||||
|
|
||||||
/*---- Public helper enumeration ----*/
|
/*---- Public helper enumeration ----*/
|
||||||
|
|
||||||
@@ -241,10 +242,10 @@ class QrCode final {
|
|||||||
* The error correction level in a QR Code symbol.
|
* The error correction level in a QR Code symbol.
|
||||||
*/
|
*/
|
||||||
public: enum class Ecc {
|
public: enum class Ecc {
|
||||||
LOW = 0 , // The QR Code can tolerate about 7% erroneous codewords
|
LOW = 0, // The QR Code can tolerate about 7% erroneous codewords
|
||||||
MEDIUM , // The QR Code can tolerate about 15% erroneous codewords
|
MEDIUM, // The QR Code can tolerate about 15% erroneous codewords
|
||||||
QUARTILE, // The QR Code can tolerate about 25% erroneous codewords
|
QUARTILE, // The QR Code can tolerate about 25% erroneous codewords
|
||||||
HIGH , // The QR Code can tolerate about 30% erroneous codewords
|
HIGH, // The QR Code can tolerate about 30% erroneous codewords
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -262,7 +263,7 @@ class QrCode final {
|
|||||||
* QR Code version is automatically chosen for the output. The ECC level of the result may be higher than
|
* QR Code version is automatically chosen for the output. The ECC level of the result may be higher than
|
||||||
* the ecl argument if it can be done without increasing the version.
|
* the ecl argument if it can be done without increasing the version.
|
||||||
*/
|
*/
|
||||||
public: static QrCode encodeText(const char *text, Ecc ecl);
|
public: static QrCode encodeText(const char* text, Ecc ecl);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -271,7 +272,7 @@ class QrCode final {
|
|||||||
* bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
|
* bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
|
||||||
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
|
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
|
||||||
*/
|
*/
|
||||||
public: static QrCode encodeBinary(const std::vector<std::uint8_t> &data, Ecc ecl);
|
public: static QrCode encodeBinary(const std::vector<std::uint8_t>& data, Ecc ecl);
|
||||||
|
|
||||||
|
|
||||||
/*---- Static factory functions (mid level) ----*/
|
/*---- Static factory functions (mid level) ----*/
|
||||||
@@ -287,8 +288,8 @@ class QrCode final {
|
|||||||
* between modes (such as alphanumeric and byte) to encode text in less space.
|
* between modes (such as alphanumeric and byte) to encode text in less space.
|
||||||
* This is a mid-level API; the high-level API is encodeText() and encodeBinary().
|
* This is a mid-level API; the high-level API is encodeText() and encodeBinary().
|
||||||
*/
|
*/
|
||||||
public: static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl,
|
public: static QrCode encodeSegments(const std::vector<QrSegment>& segs, Ecc ecl,
|
||||||
int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true); // All optional parameters
|
int minVersion = 1, int maxVersion = 40, int mask = -1, bool boostEcl = true); // All optional parameters
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -331,7 +332,7 @@ class QrCode final {
|
|||||||
* This is a low-level API that most users should not use directly.
|
* This is a low-level API that most users should not use directly.
|
||||||
* A mid-level API is the encodeSegments() function.
|
* A mid-level API is the encodeSegments() function.
|
||||||
*/
|
*/
|
||||||
public: QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords, int msk);
|
public: QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t>& dataCodewords, int msk);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -409,12 +410,12 @@ class QrCode final {
|
|||||||
|
|
||||||
// Returns a new byte string representing the given data with the appropriate error correction
|
// Returns a new byte string representing the given data with the appropriate error correction
|
||||||
// codewords appended to it, based on this object's version and error correction level.
|
// codewords appended to it, based on this object's version and error correction level.
|
||||||
private: std::vector<std::uint8_t> addEccAndInterleave(const std::vector<std::uint8_t> &data) const;
|
private: std::vector<std::uint8_t> addEccAndInterleave(const std::vector<std::uint8_t>& data) const;
|
||||||
|
|
||||||
|
|
||||||
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
// Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
|
||||||
// data area of this QR Code. Function modules need to be marked off before this is called.
|
// data area of this QR Code. Function modules need to be marked off before this is called.
|
||||||
private: void drawCodewords(const std::vector<std::uint8_t> &data);
|
private: void drawCodewords(const std::vector<std::uint8_t>& data);
|
||||||
|
|
||||||
|
|
||||||
// XORs the codeword modules in this QR Code with the given mask pattern.
|
// XORs the codeword modules in this QR Code with the given mask pattern.
|
||||||
@@ -457,7 +458,7 @@ class QrCode final {
|
|||||||
|
|
||||||
|
|
||||||
// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
// Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
||||||
private: static std::vector<std::uint8_t> reedSolomonComputeRemainder(const std::vector<std::uint8_t> &data, const std::vector<std::uint8_t> &divisor);
|
private: static std::vector<std::uint8_t> reedSolomonComputeRemainder(const std::vector<std::uint8_t>& data, const std::vector<std::uint8_t>& divisor);
|
||||||
|
|
||||||
|
|
||||||
// Returns the product of the two given field elements modulo GF(2^8/0x11D).
|
// Returns the product of the two given field elements modulo GF(2^8/0x11D).
|
||||||
@@ -467,15 +468,15 @@ class QrCode final {
|
|||||||
|
|
||||||
// Can only be called immediately after a light run is added, and
|
// Can only be called immediately after a light run is added, and
|
||||||
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
// returns either 0, 1, or 2. A helper function for getPenaltyScore().
|
||||||
private: int finderPenaltyCountPatterns(const std::array<int,7> &runHistory) const;
|
private: int finderPenaltyCountPatterns(const std::array<int, 7>& runHistory) const;
|
||||||
|
|
||||||
|
|
||||||
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
// Must be called at the end of a line (row or column) of modules. A helper function for getPenaltyScore().
|
||||||
private: int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int,7> &runHistory) const;
|
private: int finderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, std::array<int, 7>& runHistory) const;
|
||||||
|
|
||||||
|
|
||||||
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
// Pushes the given value to the front and drops the last value. A helper function for getPenaltyScore().
|
||||||
private: void finderPenaltyAddHistory(int currentRunLength, std::array<int,7> &runHistory) const;
|
private: void finderPenaltyAddHistory(int currentRunLength, std::array<int, 7>& runHistory) const;
|
||||||
|
|
||||||
|
|
||||||
// Returns true iff the i'th bit of x is set to 1.
|
// Returns true iff the i'th bit of x is set to 1.
|
||||||
@@ -501,13 +502,13 @@ class QrCode final {
|
|||||||
private: static const std::int8_t ECC_CODEWORDS_PER_BLOCK[4][41];
|
private: static const std::int8_t ECC_CODEWORDS_PER_BLOCK[4][41];
|
||||||
private: static const std::int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];
|
private: static const std::int8_t NUM_ERROR_CORRECTION_BLOCKS[4][41];
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*---- Public exception class ----*/
|
/*---- Public exception class ----*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include:
|
* Thrown when the supplied data does not fit any QR Code version. Ways to handle this exception include:
|
||||||
* - Decrease the error correction level if it was greater than Ecc::LOW.
|
* - Decrease the error correction level if it was greater than Ecc::LOW.
|
||||||
* - If the encodeSegments() function was called with a maxVersion argument, then increase
|
* - If the encodeSegments() function was called with a maxVersion argument, then increase
|
||||||
@@ -518,18 +519,18 @@ class QrCode final {
|
|||||||
* - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).
|
* - Change the text to fit the character set of a particular segment mode (e.g. alphanumeric).
|
||||||
* - Propagate the error upward to the caller/user.
|
* - Propagate the error upward to the caller/user.
|
||||||
*/
|
*/
|
||||||
class data_too_long : public std::length_error {
|
class data_too_long : public std::length_error {
|
||||||
|
|
||||||
public: explicit data_too_long(const std::string &msg);
|
public: explicit data_too_long(const std::string& msg);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
|
* An appendable sequence of bits (0s and 1s). Mainly used by QrSegment.
|
||||||
*/
|
*/
|
||||||
class BitBuffer final : public std::vector<bool> {
|
class BitBuffer final : public std::vector<bool> {
|
||||||
|
|
||||||
/*---- Constructor ----*/
|
/*---- Constructor ----*/
|
||||||
|
|
||||||
@@ -544,6 +545,6 @@ class BitBuffer final : public std::vector<bool> {
|
|||||||
// to this buffer. Requires 0 <= len <= 31 and val < 2^len.
|
// to this buffer. Requires 0 <= len <= 31 and val < 2^len.
|
||||||
public: void appendBits(std::uint32_t val, int len);
|
public: void appendBits(std::uint32_t val, int len);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <climits>
|
#include <climits>
|
||||||
|
|
||||||
std::string toSvgString(const qrcodegen::QrCode &qr, int border) {
|
std::string toSvgString(const qrcodegen::QrCode& qr, int border)
|
||||||
|
{
|
||||||
if (border < 0)
|
if (border < 0)
|
||||||
throw std::domain_error("Border must be non-negative");
|
throw std::domain_error("Border must be non-negative");
|
||||||
if (border > INT_MAX / 2 || border * 2 > INT_MAX - qr.getSize())
|
if (border > INT_MAX / 2 || border * 2 > INT_MAX - qr.getSize())
|
||||||
|
|||||||
@@ -4,6 +4,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "qrcodegen.hpp"
|
#include "qrcodegen.hpp"
|
||||||
|
|
||||||
std::string toSvgString(const qrcodegen::QrCode &qr, int border);
|
std::string toSvgString(const qrcodegen::QrCode& qr, int border);
|
||||||
|
|
||||||
#endif // RTOSVG_HPP
|
#endif // RTOSVG_HPP
|
||||||
+3512
-2799
File diff suppressed because it is too large.
Load diff
+2638
-2578
File diff suppressed because it is too large.
Load diff
@@ -5,7 +5,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace security
|
namespace security
|
||||||
{
|
{
|
||||||
void set_cors_headers(const httplib::Request &request, httplib::Response &response)
|
void set_cors_headers(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << " " << std::endl;
|
std::cout << GREEN << request.path << " " << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace api
|
|||||||
namespace security
|
namespace security
|
||||||
{
|
{
|
||||||
// function to set cors headers
|
// function to set cors headers
|
||||||
void set_cors_headers(const httplib::Request &request, httplib::Response &response);
|
void set_cors_headers(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace dns
|
namespace dns
|
||||||
{
|
{
|
||||||
void get_dns(const httplib::Request &request, httplib::Response &response);
|
void get_dns(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_dns(const httplib::Request &request, httplib::Response &response);
|
void set_dns(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_dns_servers(const httplib::Request &request, httplib::Response &response);
|
void get_dns_servers(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_dns_servers(const httplib::Request &request, httplib::Response &response);
|
void set_dns_servers(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_local_domains(const httplib::Request &request, httplib::Response &response);
|
void get_local_domains(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_local_domains(const httplib::Request &request, httplib::Response &response);
|
void set_local_domains(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace dns
|
namespace dns
|
||||||
{
|
{
|
||||||
void get_dns(const httplib::Request &request, httplib::Response &response)
|
void get_dns(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -37,7 +37,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_dns(const httplib::Request &request, httplib::Response &response)
|
void set_dns(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace dns
|
namespace dns
|
||||||
{
|
{
|
||||||
void get_local_domains(const httplib::Request &request, httplib::Response &response)
|
void get_local_domains(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -36,7 +36,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_local_domains(const httplib::Request &request, httplib::Response &response)
|
void set_local_domains(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace dns
|
namespace dns
|
||||||
{
|
{
|
||||||
void get_dns_servers(const httplib::Request &request, httplib::Response &response)
|
void get_dns_servers(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -36,7 +36,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_dns_servers(const httplib::Request &request, httplib::Response &response)
|
void set_dns_servers(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -54,7 +54,7 @@ namespace api
|
|||||||
}
|
}
|
||||||
std::ofstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf");
|
std::ofstream dnsserversfile("/etc/dnsmasq.conf.d/resolv.conf");
|
||||||
nlohmann::json servers = json_body["servers"];
|
nlohmann::json servers = json_body["servers"];
|
||||||
for (const nlohmann::json_abi_v3_11_3::json &server : servers)
|
for (const nlohmann::json_abi_v3_11_3::json& server : servers)
|
||||||
{
|
{
|
||||||
std::string srv = server;
|
std::string srv = server;
|
||||||
if (server == "")
|
if (server == "")
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace ftp
|
namespace ftp
|
||||||
{
|
{
|
||||||
void get_ftp(const httplib::Request &request, httplib::Response &response)
|
void get_ftp(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -37,7 +37,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_ftp(const httplib::Request &request, httplib::Response &response)
|
void set_ftp(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace ftp
|
namespace ftp
|
||||||
{
|
{
|
||||||
void get_ftp(const httplib::Request &request, httplib::Response &response);
|
void get_ftp(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_ftp(const httplib::Request &request, httplib::Response &response);
|
void set_ftp(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace ssh
|
namespace ssh
|
||||||
{
|
{
|
||||||
void get_ssh_keys(const httplib::Request &request, httplib::Response &response)
|
void get_ssh_keys(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -38,7 +38,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_ssh_keys(const httplib::Request &request, httplib::Response &response)
|
void set_ssh_keys(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace ssh
|
namespace ssh
|
||||||
{
|
{
|
||||||
void get_ssh_port(const httplib::Request &request, httplib::Response &response)
|
void get_ssh_port(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -42,7 +42,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_ssh_port(const httplib::Request &request, httplib::Response &response)
|
void set_ssh_port(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -76,7 +76,8 @@ namespace api
|
|||||||
if (get_ssh_server() == DROPBEAR_SSH)
|
if (get_ssh_server() == DROPBEAR_SSH)
|
||||||
{
|
{
|
||||||
systemrequest << "sed -i \"s/DROPBEAR_PORT=" << old_ssh_port << "/DROPBEAR_PORT=" << new_ssh_port << "/g\" /etc/default/dropbear";
|
systemrequest << "sed -i \"s/DROPBEAR_PORT=" << old_ssh_port << "/DROPBEAR_PORT=" << new_ssh_port << "/g\" /etc/default/dropbear";
|
||||||
} else if (get_ssh_server() == OPENSSH_SSH)
|
}
|
||||||
|
else if (get_ssh_server() == OPENSSH_SSH)
|
||||||
{
|
{
|
||||||
systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config";
|
systemrequest << "sed -i \"/Port " << old_ssh_port << "/s//Port " << new_ssh_port << "/\" /etc/ssh/sshd_config";
|
||||||
}
|
}
|
||||||
@@ -85,7 +86,8 @@ namespace api
|
|||||||
if (get_ssh_server() == OPENSSH_SSH)
|
if (get_ssh_server() == OPENSSH_SSH)
|
||||||
{
|
{
|
||||||
system("rc restart ssh");
|
system("rc restart ssh");
|
||||||
} else if (get_ssh_server() == DROPBEAR_SSH)
|
}
|
||||||
|
else if (get_ssh_server() == DROPBEAR_SSH)
|
||||||
{
|
{
|
||||||
system("rc restart dropbear");
|
system("rc restart dropbear");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace api
|
|||||||
return config::config.getConfig()["settings"]["ssh"]["server"];
|
return config::config.getConfig()["settings"]["ssh"]["server"];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string set_ssh_server(std::string &server)
|
std::string set_ssh_server(std::string& server)
|
||||||
{
|
{
|
||||||
config::config.setConfig(std::string("../config.json"));
|
config::config.setConfig(std::string("../config.json"));
|
||||||
nlohmann::json new_config = config::config.getConfig();
|
nlohmann::json new_config = config::config.getConfig();
|
||||||
@@ -30,7 +30,7 @@ namespace api
|
|||||||
return config::config.getConfig()["settings"]["ssh"]["server"];
|
return config::config.getConfig()["settings"]["ssh"]["server"];
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_ssh_server_http(const httplib::Request &request, httplib::Response &response)
|
void get_ssh_server_http(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -50,7 +50,7 @@ namespace api
|
|||||||
responsedata << R"({"server":")" << get_ssh_server() << R"("})";
|
responsedata << R"({"server":")" << get_ssh_server() << R"("})";
|
||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
void set_ssh_server_http(const httplib::Request &request, httplib::Response &response)
|
void set_ssh_server_http(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -81,7 +81,8 @@ namespace api
|
|||||||
system("rc del ssh");
|
system("rc del ssh");
|
||||||
system("rc start dropbear");
|
system("rc start dropbear");
|
||||||
system("rc add dropbear");
|
system("rc add dropbear");
|
||||||
} else if (new_server == OPENSSH_SSH) {
|
}
|
||||||
|
else if (new_server == OPENSSH_SSH) {
|
||||||
system("rc stop dropbear");
|
system("rc stop dropbear");
|
||||||
system("rc del dropbear");
|
system("rc del dropbear");
|
||||||
system("rc start ssh");
|
system("rc start ssh");
|
||||||
|
|||||||
@@ -13,19 +13,19 @@ namespace api
|
|||||||
namespace ssh
|
namespace ssh
|
||||||
{
|
{
|
||||||
std::string get_ssh_server();
|
std::string get_ssh_server();
|
||||||
std::string set_ssh_server(std::string &server);
|
std::string set_ssh_server(std::string& server);
|
||||||
|
|
||||||
void get_ssh_server_http(const httplib::Request &request, httplib::Response &response);
|
void get_ssh_server_http(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_ssh_server_http(const httplib::Request &request, httplib::Response &response);
|
void set_ssh_server_http(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_ssh_status(const httplib::Request &request, httplib::Response &response);
|
void get_ssh_status(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_ssh_status(const httplib::Request &request, httplib::Response &response);
|
void set_ssh_status(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_ssh_keys(const httplib::Request &request, httplib::Response &response);
|
void get_ssh_keys(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_ssh_keys(const httplib::Request &request, httplib::Response &response);
|
void set_ssh_keys(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_ssh_port(const httplib::Request &request, httplib::Response &response);
|
void get_ssh_port(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_ssh_port(const httplib::Request &request, httplib::Response &response);
|
void set_ssh_port(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace ssh
|
namespace ssh
|
||||||
{
|
{
|
||||||
void get_ssh_status(const httplib::Request &request, httplib::Response &response)
|
void get_ssh_status(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -37,7 +37,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_ssh_status(const httplib::Request &request, httplib::Response &response)
|
void set_ssh_status(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -66,7 +66,8 @@ namespace api
|
|||||||
{
|
{
|
||||||
system("rc stop dropbear");
|
system("rc stop dropbear");
|
||||||
system("rc del dropbear");
|
system("rc del dropbear");
|
||||||
} else if (get_ssh_server() == OPENSSH_SSH) {
|
}
|
||||||
|
else if (get_ssh_server() == OPENSSH_SSH) {
|
||||||
system("rc stop ssh");
|
system("rc stop ssh");
|
||||||
system("rc del ssh");
|
system("rc del ssh");
|
||||||
}
|
}
|
||||||
@@ -78,7 +79,8 @@ namespace api
|
|||||||
{
|
{
|
||||||
system("rc start dropbear");
|
system("rc start dropbear");
|
||||||
system("rc add dropbear");
|
system("rc add dropbear");
|
||||||
} else if (get_ssh_server() == OPENSSH_SSH) {
|
}
|
||||||
|
else if (get_ssh_server() == OPENSSH_SSH) {
|
||||||
system("rc start ssh");
|
system("rc start ssh");
|
||||||
system("rc add ssh");
|
system("rc add ssh");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace telnet
|
namespace telnet
|
||||||
{
|
{
|
||||||
void get_telnet(const httplib::Request &request, httplib::Response &response)
|
void get_telnet(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -41,7 +41,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_telnet(const httplib::Request &request, httplib::Response &response)
|
void set_telnet(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace telnet
|
namespace telnet
|
||||||
{
|
{
|
||||||
void get_telnet_port(const httplib::Request &request, httplib::Response &response)
|
void get_telnet_port(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -41,7 +41,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_telnet_port(const httplib::Request &request, httplib::Response &response)
|
void set_telnet_port(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace telnet
|
namespace telnet
|
||||||
{
|
{
|
||||||
void get_telnet(const httplib::Request &request, httplib::Response &response);
|
void get_telnet(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_telnet(const httplib::Request &request, httplib::Response &response);
|
void set_telnet(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_telnet_port(const httplib::Request &request, httplib::Response &response);
|
void get_telnet_port(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_telnet_port(const httplib::Request &request, httplib::Response &response);
|
void set_telnet_port(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace wifi
|
namespace wifi
|
||||||
{
|
{
|
||||||
void get_wifi_channel(const httplib::Request &request, httplib::Response &response)
|
void get_wifi_channel(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -41,7 +41,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_wifi_channel(const httplib::Request &request, httplib::Response &response)
|
void set_wifi_channel(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace wifi
|
namespace wifi
|
||||||
{
|
{
|
||||||
void get_wifi_name(const httplib::Request &request, httplib::Response &response)
|
void get_wifi_name(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -41,7 +41,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_wifi_name(const httplib::Request &request, httplib::Response &response)
|
void set_wifi_name(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace wifi
|
namespace wifi
|
||||||
{
|
{
|
||||||
void get_wifi_password(const httplib::Request &request, httplib::Response &response)
|
void get_wifi_password(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -41,7 +41,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_wifi_password(const httplib::Request &request, httplib::Response &response)
|
void set_wifi_password(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace wifi
|
namespace wifi
|
||||||
{
|
{
|
||||||
void get_wifi_qr(const httplib::Request &request, httplib::Response &response)
|
void get_wifi_qr(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace wifi
|
namespace wifi
|
||||||
{
|
{
|
||||||
void get_wifi_status(const httplib::Request &request, httplib::Response &response)
|
void get_wifi_status(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
@@ -37,7 +37,7 @@ namespace api
|
|||||||
response.set_content(responsedata.str(), JSON_TYPE);
|
response.set_content(responsedata.str(), JSON_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_wifi_status(const httplib::Request &request, httplib::Response &response)
|
void set_wifi_status(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -11,19 +11,19 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace wifi
|
namespace wifi
|
||||||
{
|
{
|
||||||
void get_wifi_status(const httplib::Request &request, httplib::Response &response);
|
void get_wifi_status(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_wifi_status(const httplib::Request &request, httplib::Response &response);
|
void set_wifi_status(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_wifi_name(const httplib::Request &request, httplib::Response &response);
|
void get_wifi_name(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_wifi_name(const httplib::Request &request, httplib::Response &response);
|
void set_wifi_name(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_wifi_password(const httplib::Request &request, httplib::Response &response);
|
void get_wifi_password(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_wifi_password(const httplib::Request &request, httplib::Response &response);
|
void set_wifi_password(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_wifi_qr(const httplib::Request &request, httplib::Response &response);
|
void get_wifi_qr(const httplib::Request& request, httplib::Response& response);
|
||||||
|
|
||||||
void get_wifi_channel(const httplib::Request &request, httplib::Response &response);
|
void get_wifi_channel(const httplib::Request& request, httplib::Response& response);
|
||||||
void set_wifi_channel(const httplib::Request &request, httplib::Response &response);
|
void set_wifi_channel(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace system
|
namespace system
|
||||||
{
|
{
|
||||||
void reboot(const httplib::Request &request, httplib::Response &response)
|
void reboot(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||||
response.set_header("Access-Control-Allow-Origin", "*");
|
response.set_header("Access-Control-Allow-Origin", "*");
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace system
|
namespace system
|
||||||
{
|
{
|
||||||
void reboot(const httplib::Request &request, httplib::Response &response);
|
void reboot(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace system
|
namespace system
|
||||||
{
|
{
|
||||||
void get_syslog(const httplib::Request &request, httplib::Response &response)
|
void get_syslog(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
std::stringstream responsedata;
|
std::stringstream responsedata;
|
||||||
std::ifstream syslogfile(SYSLOG_FILE);
|
std::ifstream syslogfile(SYSLOG_FILE);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace system
|
namespace system
|
||||||
{
|
{
|
||||||
void get_syslog(const httplib::Request &request, httplib::Response &response);
|
void get_syslog(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace system
|
namespace system
|
||||||
{
|
{
|
||||||
void get_uptime(const httplib::Request &request, httplib::Response &response)
|
void get_uptime(const httplib::Request& request, httplib::Response& response)
|
||||||
{
|
{
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
struct sysinfo x;
|
struct sysinfo x;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace api
|
|||||||
{
|
{
|
||||||
namespace system
|
namespace system
|
||||||
{
|
{
|
||||||
void get_uptime(const httplib::Request &request, httplib::Response &response);
|
void get_uptime(const httplib::Request& request, httplib::Response& response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user