Skip to content

Commit 73e9bcc

Browse files
committed
tidy:1.8.14, 优化base64的计算函数
1 parent 2da184b commit 73e9bcc

File tree

9 files changed

+291
-83
lines changed

9 files changed

+291
-83
lines changed

LICENSE

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
MIT License
22

3+
.============.
4+
// M A K E / \
5+
// C++ DEV / \
6+
// E A S Y / \/ \
7+
++ ----------. \/\ .
8+
\\ \ \ /\ /
9+
\\ \ \ /
10+
\\ \ \ /
11+
-============'
12+
313
Copyright (c) 2018 Hevake Lee <hevake_lcj@126.com>
414

515
Permission is hereby granted, free of charge, to any person obtaining a copy

build_env.mk

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
#
2+
# .============.
3+
# // M A K E / \
4+
# // C++ DEV / \
5+
# // E A S Y / \/ \
6+
# ++ ----------. \/\ .
7+
# \\ \ \ /\ /
8+
# \\ \ \ /
9+
# \\ \ \ /
10+
# -============'
11+
#
12+
# Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
#
14+
# This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
# Use of this source code is governed by MIT license that can be found
16+
# in the LICENSE file in the root of the source tree. All contributing
17+
# project authors may be found in the CONTRIBUTORS.md file in the root
18+
# of the source tree.
19+
#
20+
121
export TOOLCHAIN_BIN_PREFIX ?=
222
export BUILD_DIR ?= $(TOP_DIR)/.build
323
export STAGING_DIR ?= $(TOP_DIR)/.staging

config.mk

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
#
2+
# .============.
3+
# // M A K E / \
4+
# // C++ DEV / \
5+
# // E A S Y / \/ \
6+
# ++ ----------. \/\ .
7+
# \\ \ \ /\ /
8+
# \\ \ \ /
9+
# \\ \ \ /
10+
# -============'
11+
#
12+
# Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
#
14+
# This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
# Use of this source code is governed by MIT license that can be found
16+
# in the LICENSE file in the root of the source tree. All contributing
17+
# project authors may be found in the CONTRIBUTORS.md file in the root
18+
# of the source tree.
19+
#
20+
121
# 核心模块
222
MODULES += base
323
MODULES += util

modules/flow/actions/function_action_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ TEST(FunctionAction, WithReason) {
9090

9191
bool is_callback = false;
9292
action.setFinishCallback(
93-
[&] (bool is_succ, const Action::Reason &r, const Action::Trace &t) {
93+
[&] (bool is_succ, const Action::Reason &r, const Action::Trace &) {
9494
EXPECT_FALSE(is_succ);
9595
EXPECT_EQ(r.code, 1001);
9696
EXPECT_EQ(r.message, "test");

modules/terminal/impl/terminal.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,29 @@ void Terminal::Impl::setOptions(const SessionToken &st, uint32_t options)
9494

9595
bool Terminal::Impl::onBegin(const SessionToken &st)
9696
{
97+
const char *welcome_str = \
98+
"\r\n"
99+
R"( .============. )""\r\n"
100+
R"( // M A K E / \ )""\r\n"
101+
R"( // C++ DEV / \ )""\r\n"
102+
R"( // E A S Y / \/ \ )""\r\n"
103+
R"( ++ ----------. \/\ . )""\r\n"
104+
R"( \\ \ \ /\ / )""\r\n"
105+
R"( \\ \ \ / )""\r\n"
106+
R"( \\ \ \ / )""\r\n"
107+
R"( -============' )""\r\n"
108+
"\r\n"
109+
"Welcome to cpp-tbox terminal.\r\n"
110+
"Type 'help' for more information.\r\n"
111+
"\r\n";
112+
97113
auto s = sessions_.at(st);
98114
if (s == nullptr)
99115
return false;
100116

101-
if (!(s->options & kQuietMode)) {
102-
s->wp_conn->send(st,
103-
"\r\n"
104-
"Welcome to CppTBox Terminal.\r\n"
105-
"Type 'help' for more information.\r\n"
106-
"\r\n"
107-
);
108-
}
117+
if (!(s->options & kQuietMode))
118+
s->wp_conn->send(st, welcome_str);
119+
109120
printPrompt(s);
110121

111122
return true;
@@ -208,7 +219,8 @@ void Terminal::Impl::printPrompt(SessionContext *s)
208219
void Terminal::Impl::printHelp(SessionContext *s)
209220
{
210221
const char *help_str = \
211-
"This terminal is designed by Hevake Lee <hevake@126.com>, integrated in CppTBox.\r\n"
222+
"This terminal is designed by Hevake Lee <hevake@126.com>, integrated in cpp-tbox.\r\n"
223+
"Repository: https://github.com/cpp-main/cpp-tbox\r\n"
212224
"It provides a way to interact with the program in the form of shell.\r\n"
213225
"\r\n"
214226
"There are some buildin commands:\r\n"

modules/util/base64.cpp

Lines changed: 113 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -95,68 +95,95 @@ const uint8_t base64de[] = {
9595
};
9696
}
9797

98-
size_t Encode(const void *in, size_t inlen, char *out, size_t outlen)
98+
size_t DecodeLength(const char *base64_ptr, size_t base64_size)
9999
{
100-
TBOX_ASSERT(in != nullptr);
101-
TBOX_ASSERT(out != nullptr);
100+
if (base64_size == 0 || (base64_size & 0x3) != 0)
101+
return 0;
102+
103+
size_t len = base64_size / 4 * 3;
104+
//! 检查字串尾部的=符号
105+
if (base64_ptr[base64_size - 1] == BASE64_PAD)
106+
--len;
107+
if (base64_ptr[base64_size - 2] == BASE64_PAD)
108+
--len;
109+
return len;
110+
}
111+
112+
size_t DecodeLength(const char *base64_str)
113+
{
114+
return DecodeLength(base64_str, ::strlen(base64_str));
115+
}
116+
117+
size_t DecodeLength(const std::string &base64_str)
118+
{
119+
return DecodeLength(base64_str.data(), base64_str.length());
120+
}
121+
122+
size_t Encode(const void *raw_data_ptr, size_t raw_data_len, char *base64_ptr, size_t base64_size)
123+
{
124+
TBOX_ASSERT(raw_data_ptr != nullptr);
125+
TBOX_ASSERT(raw_data_len > 0);
126+
TBOX_ASSERT(base64_ptr != nullptr);
127+
TBOX_ASSERT(base64_size > 0);
102128

103-
if (EncodeLength(inlen) > outlen)
129+
if (EncodeLength(raw_data_len) > base64_size)
104130
return 0;
105131

106132
int s = 0;
107133
uint8_t l = 0;
108134
size_t w_pos = 0;
109-
const uint8_t *in_bytes = static_cast<const uint8_t*>(in);
135+
const uint8_t *in_bytes = static_cast<const uint8_t*>(raw_data_ptr);
110136

111-
for (size_t r_pos = 0; r_pos < inlen; r_pos++) {
137+
for (size_t r_pos = 0; r_pos < raw_data_len; r_pos++) {
112138
uint8_t c = in_bytes[r_pos];
113139

114140
switch (s) {
115141
case 0:
116142
s = 1;
117-
out[w_pos++] = base64en[(c >> 2) & 0x3F];
143+
base64_ptr[w_pos++] = base64en[(c >> 2) & 0x3F];
118144
break;
119145
case 1:
120146
s = 2;
121-
out[w_pos++] = base64en[((l & 0x3) << 4) | ((c >> 4) & 0xF)];
147+
base64_ptr[w_pos++] = base64en[((l & 0x3) << 4) | ((c >> 4) & 0xF)];
122148
break;
123149
case 2:
124150
s = 0;
125-
out[w_pos++] = base64en[((l & 0xF) << 2) | ((c >> 6) & 0x3)];
126-
out[w_pos++] = base64en[c & 0x3F];
151+
base64_ptr[w_pos++] = base64en[((l & 0xF) << 2) | ((c >> 6) & 0x3)];
152+
base64_ptr[w_pos++] = base64en[c & 0x3F];
127153
break;
128154
}
129155
l = c;
130156
}
131157

132158
switch (s) {
133159
case 1:
134-
out[w_pos++] = base64en[(l & 0x3) << 4];
135-
out[w_pos++] = BASE64_PAD;
136-
out[w_pos++] = BASE64_PAD;
160+
base64_ptr[w_pos++] = base64en[(l & 0x3) << 4];
161+
base64_ptr[w_pos++] = BASE64_PAD;
162+
base64_ptr[w_pos++] = BASE64_PAD;
137163
break;
138164
case 2:
139-
out[w_pos++] = base64en[(l & 0xF) << 2];
140-
out[w_pos++] = BASE64_PAD;
165+
base64_ptr[w_pos++] = base64en[(l & 0xF) << 2];
166+
base64_ptr[w_pos++] = BASE64_PAD;
141167
break;
142168
}
143169

144170
return w_pos;
145171
}
146172

147-
std::string Encode(const void *in, size_t inlen)
173+
std::string Encode(const void *raw_data_ptr, size_t raw_data_len)
148174
{
149-
TBOX_ASSERT(in != nullptr);
175+
TBOX_ASSERT(raw_data_ptr != nullptr);
176+
TBOX_ASSERT(raw_data_len > 0);
150177

151178
std::string base64_str;
152-
auto base64_len = EncodeLength(inlen);
179+
auto base64_len = EncodeLength(raw_data_len);
153180
base64_str.reserve(base64_len);
154181

155182
uint8_t l = 0;
156183
uint8_t s = 0;
157-
const uint8_t *in_bytes = static_cast<const uint8_t*>(in);
184+
const uint8_t *in_bytes = static_cast<const uint8_t*>(raw_data_ptr);
158185

159-
for (size_t r_pos = 0; r_pos < inlen; ++r_pos) {
186+
for (size_t r_pos = 0; r_pos < raw_data_len; ++r_pos) {
160187
uint8_t c = in_bytes[r_pos];
161188

162189
switch (s) {
@@ -192,72 +219,104 @@ std::string Encode(const void *in, size_t inlen)
192219
return base64_str;
193220
}
194221

195-
size_t Decode(const char *in, size_t inlen, void *out, size_t outlen)
222+
std::string Encode(const std::vector<uint8_t> &raw_data)
223+
{
224+
return Encode(raw_data.data(), raw_data.size());
225+
}
226+
227+
size_t Decode(const char *base64_ptr, size_t base64_len, void *raw_data_ptr, size_t raw_data_size)
196228
{
197-
TBOX_ASSERT(in != nullptr);
198-
TBOX_ASSERT(out != nullptr);
229+
TBOX_ASSERT(base64_ptr != nullptr);
230+
TBOX_ASSERT(raw_data_ptr != nullptr);
199231

200-
if (inlen & 0x3)
232+
if (base64_len & 0x3)
201233
return 0;
202234

203-
if (DecodeLength(in, inlen) > outlen)
235+
if (DecodeLength(base64_ptr, base64_len) > raw_data_size)
204236
return 0;
205237

206238
size_t w_pos = 0;
207-
uint8_t *out_bytes = static_cast<uint8_t*>(out);
239+
uint8_t *out_bytes = static_cast<uint8_t*>(raw_data_ptr);
208240

209-
for (size_t r_pos = 0; r_pos < inlen; r_pos++) {
210-
char in_c = in[r_pos];
211-
if (in_c == BASE64_PAD)
241+
for (size_t r_pos = 0; r_pos < base64_len; r_pos++) {
242+
char c = base64_ptr[r_pos];
243+
if (c == BASE64_PAD)
212244
break;
213245

214-
uint8_t c = base64de[int(in_c)];
215-
if (c == 255)
246+
uint8_t v = base64de[int(c)];
247+
if (v == 255)
216248
return 0;
217249

218250
switch (r_pos & 0x3) {
219251
case 0:
220-
out_bytes[w_pos] = (c << 2) & 0xFF;
252+
out_bytes[w_pos] = v << 2;
221253
break;
222254
case 1:
223-
out_bytes[w_pos++] |= (c >> 4) & 0x3;
224-
out_bytes[w_pos] = (c & 0xF) << 4;
255+
out_bytes[w_pos++] |= v >> 4;
256+
out_bytes[w_pos] = v << 4;
225257
break;
226258
case 2:
227-
out_bytes[w_pos++] |= (c >> 2) & 0xF;
228-
out_bytes[w_pos] = (c & 0x3) << 6;
259+
out_bytes[w_pos++] |= v >> 2;
260+
out_bytes[w_pos] = v << 6;
229261
break;
230262
case 3:
231-
out_bytes[w_pos++] |= c;
263+
out_bytes[w_pos++] |= v;
232264
break;
233265
}
234266
}
235267

236268
return w_pos;
237269
}
238270

239-
size_t DecodeLength(const char *encode_str, size_t encode_str_len)
271+
size_t Decode(const char *base64_str, void *raw_data_ptr, size_t raw_data_size)
240272
{
241-
if (encode_str_len == 0 || (encode_str_len & 0x3) != 0)
242-
return 0;
243-
244-
size_t len = encode_str_len / 4 * 3;
245-
//! 检查字串尾部的=符号
246-
if (encode_str[encode_str_len - 1] == BASE64_PAD)
247-
--len;
248-
if (encode_str[encode_str_len - 2] == BASE64_PAD)
249-
--len;
250-
return len;
273+
return Decode(base64_str, ::strlen(base64_str), raw_data_ptr, raw_data_size);
251274
}
252275

253-
size_t DecodeLength(const char *encode_str)
276+
size_t Decode(const std::string &base64_str, std::vector<uint8_t> &raw_data)
254277
{
255-
return DecodeLength(encode_str, ::strlen(encode_str));
256-
}
278+
size_t out_len = DecodeLength(base64_str);
279+
if (out_len == 0)
280+
return 0;
257281

258-
size_t DecodeLength(const std::string &encode_str)
259-
{
260-
return DecodeLength(encode_str.data(), encode_str.length());
282+
raw_data.reserve(raw_data.size() + out_len);
283+
284+
uint8_t tmp = 0;
285+
uint8_t s = 0;
286+
287+
for (char c : base64_str) {
288+
if (c == BASE64_PAD)
289+
break;
290+
291+
uint8_t v = base64de[int(c)];
292+
if (v == 255)
293+
return 0;
294+
295+
switch (s) {
296+
case 0:
297+
tmp = v << 2;
298+
break;
299+
case 1:
300+
tmp |= v >> 4;
301+
raw_data.push_back(tmp);
302+
tmp = v << 4;
303+
break;
304+
case 2:
305+
tmp |= v >> 2;
306+
raw_data.push_back(tmp);
307+
tmp = v << 6;
308+
break;
309+
case 3:
310+
tmp |= v;
311+
raw_data.push_back(tmp);
312+
break;
313+
}
314+
315+
++s;
316+
s &= 0x03;
317+
}
318+
319+
return out_len;
261320
}
262321

263322
}

0 commit comments

Comments
 (0)