2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/base64.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
//TODO: fix copypaste
|
|
|
|
|
|
|
|
static const char *const symbols64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
|
|
|
string base64_encode(Slice input) {
|
|
|
|
string base64;
|
|
|
|
base64.reserve((input.size() + 2) / 3 * 4);
|
|
|
|
for (size_t i = 0; i < input.size();) {
|
2018-02-12 11:37:54 +01:00
|
|
|
size_t left = min(input.size() - i, static_cast<size_t>(3));
|
2018-01-19 13:43:00 +01:00
|
|
|
int c = input.ubegin()[i++] << 16;
|
2018-12-31 20:04:05 +01:00
|
|
|
base64 += symbols64[c >> 18];
|
|
|
|
if (left != 1) {
|
|
|
|
c |= input.ubegin()[i++] << 8;
|
|
|
|
}
|
|
|
|
base64 += symbols64[(c >> 12) & 63];
|
|
|
|
if (left == 3) {
|
|
|
|
c |= input.ubegin()[i++];
|
|
|
|
}
|
|
|
|
if (left != 1) {
|
|
|
|
base64 += symbols64[(c >> 6) & 63];
|
|
|
|
} else {
|
|
|
|
base64 += '=';
|
|
|
|
}
|
|
|
|
if (left == 3) {
|
|
|
|
base64 += symbols64[c & 63];
|
|
|
|
} else {
|
|
|
|
base64 += '=';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char char_to_value[256];
|
2018-01-19 13:43:00 +01:00
|
|
|
static void init_base64_table() {
|
2019-08-12 13:45:57 +02:00
|
|
|
static bool is_inited = [] {
|
2018-04-11 22:17:51 +02:00
|
|
|
std::fill(std::begin(char_to_value), std::end(char_to_value), static_cast<unsigned char>(64));
|
2018-01-19 13:43:00 +01:00
|
|
|
for (unsigned char i = 0; i < 64; i++) {
|
|
|
|
char_to_value[static_cast<size_t>(symbols64[i])] = i;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}();
|
2018-01-23 17:45:51 +01:00
|
|
|
CHECK(is_inited);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
Result<Slice> base64_drop_padding(Slice base64) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if ((base64.size() & 3) != 0) {
|
|
|
|
return Status::Error("Wrong string length");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t padding_length = 0;
|
2018-01-19 13:43:00 +01:00
|
|
|
while (!base64.empty() && base64.back() == '=') {
|
2018-12-31 20:04:05 +01:00
|
|
|
base64.remove_suffix(1);
|
|
|
|
padding_length++;
|
|
|
|
}
|
|
|
|
if (padding_length >= 3) {
|
|
|
|
return Status::Error("Wrong string padding");
|
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
return base64;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
template <class F>
|
|
|
|
Status base64_do_decode(Slice base64, F &&append) {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (size_t i = 0; i < base64.size();) {
|
2018-02-12 11:37:54 +01:00
|
|
|
size_t left = min(base64.size() - i, static_cast<size_t>(4));
|
2018-01-19 13:43:00 +01:00
|
|
|
int c = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (size_t t = 0; t < left; t++) {
|
|
|
|
auto value = char_to_value[base64.ubegin()[i++]];
|
|
|
|
if (value == 64) {
|
|
|
|
return Status::Error("Wrong character in the string");
|
|
|
|
}
|
|
|
|
c |= value << ((3 - t) * 6);
|
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
append(static_cast<char>(static_cast<unsigned char>(c >> 16))); // implementation-defined
|
2018-12-31 20:04:05 +01:00
|
|
|
if (left == 2) {
|
|
|
|
if ((c & ((1 << 16) - 1)) != 0) {
|
|
|
|
return Status::Error("Wrong padding in the string");
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-12 13:45:57 +02:00
|
|
|
append(static_cast<char>(static_cast<unsigned char>(c >> 8))); // implementation-defined
|
2018-12-31 20:04:05 +01:00
|
|
|
if (left == 3) {
|
|
|
|
if ((c & ((1 << 8) - 1)) != 0) {
|
|
|
|
return Status::Error("Wrong padding in the string");
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-12 13:45:57 +02:00
|
|
|
append(static_cast<char>(static_cast<unsigned char>(c))); // implementation-defined
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<string> base64_decode(Slice base64) {
|
|
|
|
init_base64_table();
|
|
|
|
|
2019-08-13 22:52:54 +02:00
|
|
|
TRY_RESULT_ASSIGN(base64, base64_drop_padding(base64));
|
2019-08-12 13:45:57 +02:00
|
|
|
|
|
|
|
string output;
|
|
|
|
output.reserve(((base64.size() + 3) >> 2) * 3);
|
|
|
|
TRY_STATUS(base64_do_decode(base64, [&output](char c) { output += c; }));
|
2018-12-31 20:04:05 +01:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
Result<SecureString> base64_decode_secure(Slice base64) {
|
|
|
|
init_base64_table();
|
|
|
|
|
2019-08-13 22:52:54 +02:00
|
|
|
TRY_RESULT_ASSIGN(base64, base64_drop_padding(base64));
|
2019-08-12 13:45:57 +02:00
|
|
|
|
|
|
|
SecureString output(((base64.size() + 3) >> 2) * 3);
|
|
|
|
char *ptr = output.as_mutable_slice().begin();
|
|
|
|
TRY_STATUS(base64_do_decode(base64, [&ptr](char c) { *ptr++ = c; }));
|
|
|
|
size_t size = ptr - output.as_mutable_slice().begin();
|
|
|
|
if (size == output.size()) {
|
|
|
|
return std::move(output);
|
|
|
|
}
|
|
|
|
return SecureString(output.as_slice().substr(0, size));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
static const char *const url_symbols64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
|
|
|
|
|
|
string base64url_encode(Slice input) {
|
|
|
|
string base64;
|
|
|
|
base64.reserve((input.size() + 2) / 3 * 4);
|
|
|
|
for (size_t i = 0; i < input.size();) {
|
2018-02-12 11:37:54 +01:00
|
|
|
size_t left = min(input.size() - i, static_cast<size_t>(3));
|
2018-01-19 13:43:00 +01:00
|
|
|
int c = input.ubegin()[i++] << 16;
|
2018-12-31 20:04:05 +01:00
|
|
|
base64 += url_symbols64[c >> 18];
|
|
|
|
if (left != 1) {
|
|
|
|
c |= input.ubegin()[i++] << 8;
|
|
|
|
}
|
|
|
|
base64 += url_symbols64[(c >> 12) & 63];
|
|
|
|
if (left == 3) {
|
|
|
|
c |= input.ubegin()[i++];
|
|
|
|
}
|
|
|
|
if (left != 1) {
|
|
|
|
base64 += url_symbols64[(c >> 6) & 63];
|
|
|
|
}
|
|
|
|
if (left == 3) {
|
|
|
|
base64 += url_symbols64[c & 63];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char url_char_to_value[256];
|
2018-01-19 13:43:00 +01:00
|
|
|
static void init_base64url_table() {
|
2019-08-12 13:45:57 +02:00
|
|
|
static bool is_inited = [] {
|
2018-10-14 01:14:19 +02:00
|
|
|
std::fill(std::begin(url_char_to_value), std::end(url_char_to_value), static_cast<unsigned char>(64));
|
2018-01-19 13:43:00 +01:00
|
|
|
for (unsigned char i = 0; i < 64; i++) {
|
|
|
|
url_char_to_value[static_cast<size_t>(url_symbols64[i])] = i;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}();
|
2018-01-23 17:45:51 +01:00
|
|
|
CHECK(is_inited);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<string> base64url_decode(Slice base64) {
|
2018-01-19 13:43:00 +01:00
|
|
|
init_base64url_table();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
size_t padding_length = 0;
|
2018-01-19 13:43:00 +01:00
|
|
|
while (!base64.empty() && base64.back() == '=') {
|
2018-12-31 20:04:05 +01:00
|
|
|
base64.remove_suffix(1);
|
|
|
|
padding_length++;
|
|
|
|
}
|
|
|
|
if (padding_length >= 3 || (padding_length > 0 && ((base64.size() + padding_length) & 3) != 0)) {
|
|
|
|
return Status::Error("Wrong string padding");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((base64.size() & 3) == 1) {
|
|
|
|
return Status::Error("Wrong string length");
|
|
|
|
}
|
|
|
|
|
|
|
|
string output;
|
|
|
|
output.reserve(((base64.size() + 3) >> 2) * 3);
|
|
|
|
for (size_t i = 0; i < base64.size();) {
|
2018-02-12 11:37:54 +01:00
|
|
|
size_t left = min(base64.size() - i, static_cast<size_t>(4));
|
2018-01-19 13:43:00 +01:00
|
|
|
int c = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (size_t t = 0; t < left; t++) {
|
|
|
|
auto value = url_char_to_value[base64.ubegin()[i++]];
|
|
|
|
if (value == 64) {
|
|
|
|
return Status::Error("Wrong character in the string");
|
|
|
|
}
|
|
|
|
c |= value << ((3 - t) * 6);
|
|
|
|
}
|
|
|
|
output += static_cast<char>(static_cast<unsigned char>(c >> 16)); // implementation-defined
|
|
|
|
if (left == 2) {
|
|
|
|
if ((c & ((1 << 16) - 1)) != 0) {
|
|
|
|
return Status::Error("Wrong padding in the string");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output += static_cast<char>(static_cast<unsigned char>(c >> 8)); // implementation-defined
|
|
|
|
if (left == 3) {
|
|
|
|
if ((c & ((1 << 8) - 1)) != 0) {
|
|
|
|
return Status::Error("Wrong padding in the string");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
output += static_cast<char>(static_cast<unsigned char>(c)); // implementation-defined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:38:56 +01:00
|
|
|
template <bool is_url>
|
|
|
|
static bool is_base64_impl(Slice input) {
|
2018-01-19 13:43:00 +01:00
|
|
|
size_t padding_length = 0;
|
|
|
|
while (!input.empty() && input.back() == '=') {
|
|
|
|
input.remove_suffix(1);
|
|
|
|
padding_length++;
|
|
|
|
}
|
|
|
|
if (padding_length >= 3) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-19 14:38:56 +01:00
|
|
|
if ((!is_url || padding_length > 0) && ((input.size() + padding_length) & 3) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (is_url && (input.size() & 3) == 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-19 13:43:00 +01:00
|
|
|
|
2018-01-19 14:38:56 +01:00
|
|
|
unsigned char *table;
|
|
|
|
if (is_url) {
|
|
|
|
init_base64url_table();
|
|
|
|
table = url_char_to_value;
|
|
|
|
} else {
|
|
|
|
init_base64_table();
|
|
|
|
table = char_to_value;
|
|
|
|
}
|
2018-01-20 12:47:53 +01:00
|
|
|
for (auto c : input) {
|
2018-01-19 14:38:56 +01:00
|
|
|
if (table[static_cast<unsigned char>(c)] == 64) {
|
2018-01-19 13:43:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((input.size() & 3) == 2) {
|
2018-01-23 17:45:51 +01:00
|
|
|
auto value = table[static_cast<int>(input.back())];
|
2018-01-19 13:43:00 +01:00
|
|
|
if ((value & 15) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((input.size() & 3) == 3) {
|
2018-01-23 17:45:51 +01:00
|
|
|
auto value = table[static_cast<int>(input.back())];
|
2018-01-19 13:43:00 +01:00
|
|
|
if ((value & 3) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:38:56 +01:00
|
|
|
bool is_base64(Slice input) {
|
|
|
|
return is_base64_impl<false>(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_base64url(Slice input) {
|
|
|
|
return is_base64_impl<true>(input);
|
|
|
|
}
|
|
|
|
|
2018-01-19 13:43:00 +01:00
|
|
|
string base64_filter(Slice input) {
|
2018-12-31 20:04:05 +01:00
|
|
|
string res;
|
2018-01-19 13:43:00 +01:00
|
|
|
res.reserve(input.size());
|
|
|
|
init_base64_table();
|
2018-01-19 14:38:56 +01:00
|
|
|
for (auto c : input) {
|
|
|
|
if (char_to_value[static_cast<unsigned char>(c)] != 64 || c == '=') {
|
|
|
|
res += c;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|