mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
bugfixes + doc update
This commit is contained in:
parent
77842f9b63
commit
1de39f5d7c
44 changed files with 652 additions and 272 deletions
|
@ -14,7 +14,7 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#include "td/utils/base64.h"
|
||||
|
||||
|
@ -30,6 +30,68 @@
|
|||
namespace td {
|
||||
//TODO: fix copypaste
|
||||
|
||||
static const char *const symbols32_lc = "abcdefghijklmnopqrstuvwxyz234567";
|
||||
static const char *const symbols32_uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
|
||||
string base32_encode(Slice input, bool upper_case) {
|
||||
auto *symbols32 = (upper_case ? symbols32_uc : symbols32_lc);
|
||||
string base32;
|
||||
base32.reserve((input.size() * 8 + 4) / 5);
|
||||
uint32 c = 0;
|
||||
uint32 length = 0;
|
||||
for (size_t i = 0; i < input.size(); i++) {
|
||||
c = (c << 8) | input.ubegin()[i];
|
||||
length += 8;
|
||||
while (length >= 5) {
|
||||
length -= 5;
|
||||
base32.push_back(symbols32[(c >> length) & 31]);
|
||||
}
|
||||
}
|
||||
if (length != 0) {
|
||||
base32.push_back(symbols32[(c << (5 - length)) & 31]);
|
||||
}
|
||||
//TODO: optional padding
|
||||
return base32;
|
||||
}
|
||||
|
||||
static unsigned char b32_char_to_value[256];
|
||||
static void init_base32_table() {
|
||||
static bool is_inited = [] {
|
||||
std::fill(std::begin(b32_char_to_value), std::end(b32_char_to_value), static_cast<unsigned char>(32));
|
||||
for (unsigned char i = 0; i < 32; i++) {
|
||||
b32_char_to_value[static_cast<size_t>(symbols32_lc[i])] = i;
|
||||
b32_char_to_value[static_cast<size_t>(symbols32_uc[i])] = i;
|
||||
}
|
||||
return true;
|
||||
}();
|
||||
CHECK(is_inited);
|
||||
}
|
||||
|
||||
Result<string> base32_decode(Slice base32) {
|
||||
init_base32_table();
|
||||
string res;
|
||||
res.reserve(base32.size() * 5 / 8);
|
||||
uint32 c = 0;
|
||||
uint32 length = 0;
|
||||
for (size_t i = 0; i < base32.size(); i++) {
|
||||
auto value = b32_char_to_value[base32.ubegin()[i]];
|
||||
if (value == 32) {
|
||||
return Status::Error("Wrong character in the string");
|
||||
}
|
||||
c = (c << 5) | value;
|
||||
length += 5;
|
||||
while (length >= 8) {
|
||||
length -= 8;
|
||||
res.push_back(td::uint8((c >> length) & 255));
|
||||
}
|
||||
}
|
||||
if ((c & ((1 << length) - 1)) != 0) {
|
||||
return Status::Error("Nonzero padding");
|
||||
}
|
||||
//TODO: check padding
|
||||
return res;
|
||||
}
|
||||
|
||||
static const char *const symbols64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
string base64_encode(Slice input) {
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Copyright 2017-2019 Telegram Systems LLP
|
||||
Copyright 2017-2020 Telegram Systems LLP
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
|
@ -37,4 +37,7 @@ bool is_base64url(Slice input);
|
|||
|
||||
string base64_filter(Slice input);
|
||||
|
||||
string base32_encode(Slice input, bool upper_case = false);
|
||||
Result<string> base32_decode(Slice base32);
|
||||
|
||||
} // namespace td
|
||||
|
|
|
@ -220,6 +220,26 @@ TEST(Misc, base64) {
|
|||
"Ojo7ISUiOw==");
|
||||
}
|
||||
|
||||
TEST(Misc, base32) {
|
||||
ASSERT_EQ("", base32_encode(""));
|
||||
ASSERT_EQ("me", base32_encode("a"));
|
||||
base32_decode("me").ensure();
|
||||
ASSERT_EQ("mfra", base32_encode("ab"));
|
||||
ASSERT_EQ("mfrgg", base32_encode("abc"));
|
||||
ASSERT_EQ("mfrggza", base32_encode("abcd"));
|
||||
ASSERT_EQ("mfrggzdg", base32_encode("abcdf"));
|
||||
ASSERT_EQ("mfrggzdgm4", base32_encode("abcdfg"));
|
||||
for (int l = 0; l < 300000; l += l / 20 + l / 1000 * 500 + 1) {
|
||||
for (int t = 0; t < 10; t++) {
|
||||
string s = rand_string(std::numeric_limits<char>::min(), std::numeric_limits<char>::max(), l);
|
||||
auto encoded = base32_encode(s);
|
||||
auto decoded = base32_decode(encoded);
|
||||
ASSERT_TRUE(decoded.is_ok());
|
||||
ASSERT_TRUE(decoded.ok() == s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Misc, to_integer) {
|
||||
ASSERT_EQ(to_integer<int32>("-1234567"), -1234567);
|
||||
ASSERT_EQ(to_integer<int64>("-1234567"), -1234567);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue