SWE-350 TOTP Generator Milestone 5
The DE-10 board has six 7-segment displays, this can be used to display and generate a time based one-time pin (TOTP).
Loading...
Searching...
No Matches
QrSegment.cpp
Go to the documentation of this file.
1/*
2 * QR Code generator library (C++)
3 *
4 * Copyright (c) Project Nayuki
5 * https://www.nayuki.io/page/qr-code-generator-library
6 *
7 * (MIT License)
8 * Permission is hereby granted, free of charge, to any person obtaining a copy of
9 * this software and associated documentation files (the "Software"), to deal in
10 * the Software without restriction, including without limitation the rights to
11 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12 * the Software, and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
14 * - The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 * - The Software is provided "as is", without warranty of any kind, express or
17 * implied, including but not limited to the warranties of merchantability,
18 * fitness for a particular purpose and noninfringement. In no event shall the
19 * authors or copyright holders be liable for any claim, damages or other
20 * liability, whether in an action of contract, tort or otherwise, arising from,
21 * out of or in connection with the Software or the use or other dealings in the
22 * Software.
23 */
24
25#include <cstddef>
26#include "BitBuffer.hpp"
27#include "QrSegment.hpp"
28
29
30qrcodegen::QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
31 modeBits(mode) {
32 numBitsCharCount[0] = cc0;
33 numBitsCharCount[1] = cc1;
34 numBitsCharCount[2] = cc2;
35}
36
37
39 if ( 1 <= ver && ver <= 9) return numBitsCharCount[0];
40 else if (10 <= ver && ver <= 26) return numBitsCharCount[1];
41 else if (27 <= ver && ver <= 40) return numBitsCharCount[2];
42 else throw "Version number out of range";
43}
44
45
50
51
52
54 return QrSegment(Mode::BYTE, data.size(), data, data.size() * 8);
55}
56
57
59 BitBuffer bb;
60 int accumData = 0;
61 int accumCount = 0;
62 int charCount = 0;
63 for (; *digits != '\0'; digits++, charCount++) {
64 char c = *digits;
65 if (c < '0' || c > '9')
66 throw "String contains non-numeric characters";
67 accumData = accumData * 10 + (c - '0');
68 accumCount++;
69 if (accumCount == 3) {
70 bb.appendBits(accumData, 10);
71 accumData = 0;
72 accumCount = 0;
73 }
74 }
75 if (accumCount > 0) // 1 or 2 digits remaining
76 bb.appendBits(accumData, accumCount * 3 + 1);
77 return QrSegment(Mode::NUMERIC, charCount, bb.getBytes(), bb.getBitLength());
78}
79
80
82 BitBuffer bb;
83 int accumData = 0;
84 int accumCount = 0;
85 int charCount = 0;
86 for (; *text != '\0'; text++, charCount++) {
87 char c = *text;
88 if (c < ' ' || c > 'Z')
89 throw "String contains unencodable characters in alphanumeric mode";
90 accumData = accumData * 45 + ALPHANUMERIC_ENCODING_TABLE[c - ' '];
91 accumCount++;
92 if (accumCount == 2) {
93 bb.appendBits(accumData, 11);
94 accumData = 0;
95 accumCount = 0;
96 }
97 }
98 if (accumCount > 0) // 1 character remaining
99 bb.appendBits(accumData, 6);
100 return QrSegment(Mode::ALPHANUMERIC, charCount, bb.getBytes(), bb.getBitLength());
101}
102
103
104std::vector<qrcodegen::QrSegment> qrcodegen::QrSegment::makeSegments(const char *text) {
105 // Select the most efficient segment encoding automatically
106 std::vector<QrSegment> result;
107 if (*text == '\0'); // Leave the vector empty
108 else if (QrSegment::isNumeric(text))
109 result.push_back(QrSegment::makeNumeric(text));
110 else if (QrSegment::isAlphanumeric(text))
111 result.push_back(QrSegment::makeAlphanumeric(text));
112 else {
113 std::vector<uint8_t> bytes;
114 for (; *text != '\0'; text++)
115 bytes.push_back(static_cast<uint8_t>(*text));
116 result.push_back(QrSegment::makeBytes(bytes));
117 }
118 return result;
119}
120
121
122qrcodegen::QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<uint8_t> &b, int bitLen) :
123 mode(md),
124 numChars(numCh),
125 data(b),
126 bitLength(bitLen) {
127 if (numCh < 0 || bitLen < 0 || b.size() != static_cast<unsigned int>((bitLen + 7) / 8))
128 throw "Invalid value";
129}
130
131
132int qrcodegen::QrSegment::getTotalBits(const std::vector<QrSegment> &segs, int version) {
133 if (version < 1 || version > 40)
134 throw "Version number out of range";
135 int result = 0;
136 for (size_t i = 0; i < segs.size(); i++) {
137 const QrSegment &seg(segs.at(i));
138 int ccbits = seg.mode.numCharCountBits(version);
139 // Fail if segment length value doesn't fit in the length field's bit-width
140 if (seg.numChars >= (1 << ccbits))
141 return -1;
142 result += 4 + ccbits + seg.bitLength;
143 }
144 return result;
145}
146
147
149 for (; *text != '\0'; text++) {
150 char c = *text;
151 if (c < ' ' || c > 'Z' || ALPHANUMERIC_ENCODING_TABLE[c - ' '] == -1)
152 return false;
153 }
154 return true;
155}
156
157
158bool qrcodegen::QrSegment::isNumeric(const char *text) {
159 for (; *text != '\0'; text++) {
160 char c = *text;
161 if (c < '0' || c > '9')
162 return false;
163 }
164 return true;
165}
166
167
168const int8_t qrcodegen::QrSegment::ALPHANUMERIC_ENCODING_TABLE[59] = {
169 // SP, !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, // ASCII codes 32 to 64
170 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, -1, // Array indices 0 to 32
171 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, // Array indices 33 to 58
172 // A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, // ASCII codes 65 to 90
173};
std::vector< uint8_t > getBytes() const
Definition BitBuffer.cpp:39
int getBitLength() const
Definition BitBuffer.cpp:34
void appendBits(uint32_t val, int len)
Definition BitBuffer.cpp:44
static const Mode ALPHANUMERIC
Definition QrSegment.hpp:55
static const Mode BYTE
Definition QrSegment.hpp:56
int numCharCountBits(int ver) const
Definition QrSegment.cpp:38
static const Mode KANJI
Definition QrSegment.hpp:57
static const Mode NUMERIC
Definition QrSegment.hpp:54
static std::vector< QrSegment > makeSegments(const char *text)
static int getTotalBits(const std::vector< QrSegment > &segs, int version)
static QrSegment makeAlphanumeric(const char *text)
Definition QrSegment.cpp:81
static QrSegment makeBytes(const std::vector< uint8_t > &data)
Definition QrSegment.cpp:53
const std::vector< uint8_t > data
static bool isAlphanumeric(const char *text)
static QrSegment makeNumeric(const char *digits)
Definition QrSegment.cpp:58
static bool isNumeric(const char *text)
QrSegment(const Mode &md, int numCh, const std::vector< uint8_t > &b, int bitLen)
uint8_t * result(void)
Definition sha1.c:104