-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQrSegment.cpp
274 lines (232 loc) · 5.99 KB
/
QrSegment.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "stdafx.h"
#include <climits>
#include <cstring>
#include <stdexcept>
#include <utility>
#include "QrSegment.hpp"
using std::uint8_t;
using std::vector;
namespace qrcodegen {
QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
modeBits(mode)
{
numBitsCharCount[0] = cc0;
numBitsCharCount[1] = cc1;
numBitsCharCount[2] = cc2;
}
// modeBits return
int QrSegment::Mode::getModeBits() const
{
return modeBits;
}
int QrSegment::Mode::numCharCountBits(int ver) const
{
return numBitsCharCount[(ver + 7) / 17];
}
const QrSegment::Mode QrSegment::Mode::NUMERIC(0x1, 10, 12, 14);
const QrSegment::Mode QrSegment::Mode::ALPHANUMERIC(0x2, 9, 11, 13);
const QrSegment::Mode QrSegment::Mode::BYTE(0x4, 8, 16, 16);
const QrSegment::Mode QrSegment::Mode::KANJI(0x8, 8, 10, 12);
const QrSegment::Mode QrSegment::Mode::ECI(0x7, 0, 0, 0);
// Returns a segment representing the given binary data encoded in byte mode
QrSegment QrSegment::makeBytes(const vector<uint8_t> &data)
{
if (data.size() > static_cast<unsigned int>(INT_MAX)) // data size too big error
{
throw std::length_error("Data too long");
}
BitBuffer bb; // 비어있는 BitBuffer 생성
for (uint8_t b : data) // byte 단위로 인코딩
{
bb.appendBits(b, 8);
}
return QrSegment(Mode::BYTE, static_cast<int>(data.size()), std::move(bb));
}
// 십진수 형태로 되어있는 string을 numeric mode로 인코딩해서 return
QrSegment QrSegment::makeNumeric(const char *digits)
{
BitBuffer bb; // 비어있는 BitBuffer 생성
int accumData = 0;
int accumCount = 0;
int charCount = 0;
for (; *digits != '\0'; digits++, charCount++)
{
char c = *digits;
if (c < '0' || c > '9') // 0-9 까지만 표현 가능
{
throw std::domain_error("String contains non-numeric characters");
}
accumData = accumData * 10 + (c - '0');
accumCount++;
if (accumCount == 3)
{
bb.appendBits(accumData, 10);
accumData = 0;
accumCount = 0;
}
}
if (accumCount > 0) // 1 or 2 digits remaining
{
bb.appendBits(accumData, accumCount * 3 + 1);
}
return QrSegment(Mode::NUMERIC, charCount, std::move(bb));
}
QrSegment QrSegment::makeAlphanumeric(const char *text)
{
BitBuffer bb;
int accumData = 0;
int accumCount = 0;
int charCount = 0;
for (; *text != '\0'; text++, charCount++)
{
const char *temp = std::strchr(ALPHANUMERIC_CHARSET, *text);
if (temp == nullptr)
{
throw std::domain_error("String contains unencodable characters in alphanumeric mode");
}
accumData = accumData * 45 + (temp - ALPHANUMERIC_CHARSET);
accumCount++;
if (accumCount == 2)
{
bb.appendBits(accumData, 11);
accumData = 0;
accumCount = 0;
}
}
if (accumCount > 0) // 1 character remaining
{
bb.appendBits(accumData, 6);
}
return QrSegment(Mode::ALPHANUMERIC, charCount, std::move(bb));
}
// Returns a list of zero or more segments to represent the given text string
vector<QrSegment> QrSegment::makeSegments(const char *text)
{
// Select the most efficient segment encoding automatically
vector<QrSegment> result;
if (*text == '\0'); // Leave result empty
else if (isNumeric(text)) // Numeric
{
result.push_back(makeNumeric(text));
}
else if (isAlphanumeric(text)) // Alphanumeric
{
result.push_back(makeAlphanumeric(text));
}
else
{
vector<uint8_t> bytes;
for (; *text != '\0'; text++)
{
bytes.push_back(static_cast<uint8_t>(*text));
}
result.push_back(makeBytes(bytes));
}
return result;
}
// Returns a segment representing an Extended Channel Interpretation (ECI) designator with the given assignment value
QrSegment QrSegment::makeEci(long assignVal)
{
BitBuffer bb;
if (assignVal < 0)
{
throw std::domain_error("ECI assignment value out of range");
}
else if (assignVal < (1 << 7))
{
bb.appendBits(assignVal, 8);
}
else if (assignVal < (1 << 14))
{
bb.appendBits(2, 2);
bb.appendBits(assignVal, 14);
}
else if (assignVal < 1000000L)
{
bb.appendBits(6, 3);
bb.appendBits(assignVal, 21);
}
else
{
throw std::domain_error("ECI assignment value out of range");
}
return QrSegment(Mode::ECI, 0, std::move(bb));
}
// Creates a new QR Code segment with the given attributes and data.
QrSegment::QrSegment(Mode md, int numCh, const std::vector<bool> &dt) :
mode(md),
numChars(numCh),
data(dt)
{
if (numCh < 0)
throw std::domain_error("Invalid value");
}
// Creates a new QR Code segment with the given attributes and data.
QrSegment::QrSegment(Mode md, int numCh, std::vector<bool> &&dt) :
mode(md),
numChars(numCh),
data(std::move(dt))
{
if (numCh < 0)
throw std::domain_error("Invalid value");
}
int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version)
{
int result = 0;
for (const QrSegment &seg : segs)
{
int ccbits = seg.mode.numCharCountBits(version);
if (seg.numChars >= (1L << ccbits))
{
return -1; // The segment's length doesn't fit the field's bit width
}
if (4 + ccbits > INT_MAX - result)
{
return -1; // The sum will overflow an int type
}
result += 4 + ccbits;
if (seg.data.size() > static_cast<unsigned int>(INT_MAX - result))
{
return -1; // The sum will overflow an int type
}
result += static_cast<int>(seg.data.size());
}
return result; // result = 4 + numCharCountBits(version) + data.size()
}
bool QrSegment::isAlphanumeric(const char *text)
{
for (; *text != '\0'; text++)
{
if (std::strchr(ALPHANUMERIC_CHARSET, *text) == nullptr)
{
return false;
}
}
return true;
}
bool QrSegment::isNumeric(const char *text)
{
for (; *text != '\0'; text++)
{
char c = *text;
if (c < '0' || c > '9')
{
return false;
}
}
return true;
}
QrSegment::Mode QrSegment::getMode() const
{
return mode;
}
int QrSegment::getNumChars() const
{
return numChars;
}
const std::vector<bool> &QrSegment::getData() const
{
return data;
}
const char *QrSegment::ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
}