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.hpp
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#pragma once
26
27#include <cstdint>
28#include <vector>
29
30
31namespace qrcodegen {
32
33/*
34 * Represents a character string to be encoded in a QR Code symbol. Each segment has
35 * a mode, and a sequence of characters that is already encoded as a sequence of bits.
36 * Instances of this class are immutable.
37 * This segment class imposes no length restrictions, but QR Codes have restrictions.
38 * Even in the most favorable conditions, a QR Code can only hold 7089 characters of data.
39 * Any segment longer than this is meaningless for the purpose of generating QR Codes.
40 */
41class QrSegment {
42
43 /*---- Public helper enumeration ----*/
44
45 /*
46 * The mode field of a segment. Immutable. Provides methods to retrieve closely related values.
47 */
48public:
49 class Mode {
50
51 /*-- Constants --*/
52 public:
53
54 static const Mode NUMERIC;
55 static const Mode ALPHANUMERIC;
56 static const Mode BYTE;
57 static const Mode KANJI;
58
59
60 /*-- Fields --*/
61
62 /* (Package-private) An unsigned 4-bit integer value (range 0 to 15) representing the mode indicator bits for this mode object. */
63 public:
64 const int modeBits;
65
66 private:
67 int numBitsCharCount[3];
68
69
70 /*-- Constructor --*/
71
72 private:
73 Mode(int mode, int cc0, int cc1, int cc2);
74
75
76 /*-- Method --*/
77
78 /*
79 * (Package-private) Returns the bit width of the segment character count field for this mode object at the given version number.
80 */
81 public:
82 int numCharCountBits(int ver) const;
83
84 };
85
86
87
88 /*---- Public static factory functions ----*/
89public:
90
91 /*
92 * Returns a segment representing the given binary data encoded in byte mode.
93 */
94 static QrSegment makeBytes(const std::vector<uint8_t> &data);
95
96
97 /*
98 * Returns a segment representing the given string of decimal digits encoded in numeric mode.
99 */
100 static QrSegment makeNumeric(const char *digits);
101
102
103 /*
104 * Returns a segment representing the given text string encoded in alphanumeric mode. The characters allowed are:
105 * 0 to 9, A to Z (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
106 */
107 static QrSegment makeAlphanumeric(const char *text);
108
109
110 /*
111 * Returns a list of zero or more segments to represent the given text string.
112 * The result may use various segment modes and switch modes to optimize the length of the bit stream.
113 */
114 static std::vector<QrSegment> makeSegments(const char *text);
115
116
117 /*---- Public static helper functions ----*/
118public:
119
120 /*
121 * Tests whether the given string can be encoded as a segment in alphanumeric mode.
122 */
123 static bool isAlphanumeric(const char *text);
124
125
126 /*
127 * Tests whether the given string can be encoded as a segment in numeric mode.
128 */
129 static bool isNumeric(const char *text);
130
131
132
133 /*---- Instance fields ----*/
134public:
135
136 /* The mode indicator for this segment. */
137 const Mode mode;
138
139 /* The length of this segment's unencoded data, measured in characters. Always zero or positive. */
140 const int numChars;
141
142 /* The bits of this segment packed into a byte array in big endian. */
143 const std::vector<uint8_t> data;
144
145 /* The length of this segment's encoded data, measured in bits. Satisfies ceil(bitLength / 8) = data.size(). */
146 const int bitLength;
147
148
149 /*---- Constructor ----*/
150public:
151
152 /*
153 * Creates a new QR Code data segment with the given parameters and data.
154 */
155 QrSegment(const Mode &md, int numCh, const std::vector<uint8_t> &b, int bitLen);
156
157
158 // Package-private helper function.
159 static int getTotalBits(const std::vector<QrSegment> &segs, int version);
160
161
162 /*---- Private constant ----*/
163private:
164
165 /* Maps shifted ASCII codes to alphanumeric mode character codes. */
166 static const int8_t ALPHANUMERIC_ENCODING_TABLE[59];
167
168};
169
170}
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)