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,12 +14,15 @@
|
|||
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 "util.h"
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include "td/utils/crypto.h"
|
||||
#include "td/utils/base64.h"
|
||||
|
||||
namespace td {
|
||||
|
||||
std::size_t compute_base64_encoded_size(size_t bindata_size) {
|
||||
|
@ -73,10 +76,6 @@ std::size_t buff_base64_encode(td::MutableSlice buffer, td::Slice raw, bool base
|
|||
return res_size;
|
||||
}
|
||||
|
||||
std::string str_base64_encode(std::string raw, bool base64_url) {
|
||||
return str_base64_encode(td::Slice{raw}, base64_url);
|
||||
}
|
||||
|
||||
std::string str_base64_encode(td::Slice raw, bool base64_url) {
|
||||
std::size_t res_size = compute_base64_encoded_size(raw.size());
|
||||
std::string s;
|
||||
|
@ -87,10 +86,6 @@ std::string str_base64_encode(td::Slice raw, bool base64_url) {
|
|||
return s;
|
||||
}
|
||||
|
||||
bool is_valid_base64(std::string encoded, bool allow_base64_url) {
|
||||
return is_valid_base64(td::Slice{encoded}, allow_base64_url);
|
||||
}
|
||||
|
||||
bool is_valid_base64(td::Slice encoded, bool allow_base64_url) {
|
||||
const unsigned char *ptr = (const unsigned char *)encoded.data(), *end = ptr + encoded.size();
|
||||
if (encoded.size() & 3) {
|
||||
|
@ -110,10 +105,6 @@ bool is_valid_base64(td::Slice encoded, bool allow_base64_url) {
|
|||
return ptr == end;
|
||||
}
|
||||
|
||||
td::int32 decoded_base64_size(std::string encoded, bool allow_base64_url) {
|
||||
return decoded_base64_size(td::Slice{encoded}, allow_base64_url);
|
||||
}
|
||||
|
||||
td::int32 decoded_base64_size(td::Slice encoded, bool allow_base64_url) {
|
||||
const unsigned char *ptr = (const unsigned char *)encoded.data(), *end = ptr + encoded.size();
|
||||
if (encoded.size() & 3) {
|
||||
|
@ -172,10 +163,6 @@ std::size_t buff_base64_decode(td::MutableSlice buffer, td::Slice encoded, bool
|
|||
return wptr - (unsigned char *)buffer.data();
|
||||
}
|
||||
|
||||
td::BufferSlice base64_decode(std::string encoded, bool allow_base64_url) {
|
||||
return base64_decode(td::Slice{encoded}, allow_base64_url);
|
||||
}
|
||||
|
||||
td::BufferSlice base64_decode(td::Slice encoded, bool allow_base64_url) {
|
||||
auto s = decoded_base64_size(encoded, allow_base64_url);
|
||||
if (s <= 0) {
|
||||
|
@ -190,10 +177,6 @@ td::BufferSlice base64_decode(td::Slice encoded, bool allow_base64_url) {
|
|||
return res;
|
||||
}
|
||||
|
||||
std::string str_base64_decode(std::string encoded, bool allow_base64_url) {
|
||||
return str_base64_decode(td::Slice{encoded}, allow_base64_url);
|
||||
}
|
||||
|
||||
std::string str_base64_decode(td::Slice encoded, bool allow_base64_url) {
|
||||
auto s = decoded_base64_size(encoded, allow_base64_url);
|
||||
if (s <= 0) {
|
||||
|
@ -209,4 +192,45 @@ std::string str_base64_decode(td::Slice encoded, bool allow_base64_url) {
|
|||
return res;
|
||||
}
|
||||
|
||||
td::Result<std::string> adnl_id_encode(td::Slice id, bool upper_case) {
|
||||
if (id.size() != 32) {
|
||||
return td::Status::Error("Wrong andl id size");
|
||||
}
|
||||
td::uint8 buf[35];
|
||||
td::MutableSlice buf_slice(buf, 35);
|
||||
buf_slice[0] = 0x2d;
|
||||
buf_slice.substr(1).copy_from(id);
|
||||
auto hash = td::crc16(buf_slice.substr(0, 33));
|
||||
buf[33] = static_cast<td::uint8>((hash >> 8) & 255);
|
||||
buf[34] = static_cast<td::uint8>(hash & 255);
|
||||
return td::base32_encode(buf_slice, upper_case).substr(1);
|
||||
}
|
||||
|
||||
std::string adnl_id_encode(td::Bits256 adnl_addr, bool upper_case) {
|
||||
return adnl_id_encode(adnl_addr.as_slice(), upper_case).move_as_ok();
|
||||
}
|
||||
|
||||
td::Result<Bits256> adnl_id_decode(td::Slice id) {
|
||||
if (id.size() != 55) {
|
||||
return td::Status::Error("Wrong length of adnl id");
|
||||
}
|
||||
td::uint8 buf[56];
|
||||
buf[0] = 'f';
|
||||
td::MutableSlice buf_slice(buf, 56);
|
||||
buf_slice.substr(1).copy_from(id);
|
||||
TRY_RESULT(decoded_str, td::base32_decode(buf_slice));
|
||||
auto decoded = td::Slice(decoded_str);
|
||||
if (decoded[0] != 0x2d) {
|
||||
return td::Status::Error("Invalid first byte");
|
||||
}
|
||||
auto got_hash = (decoded.ubegin()[33] << 8) | decoded.ubegin()[34];
|
||||
auto hash = td::crc16(decoded.substr(0, 33));
|
||||
if (hash != got_hash) {
|
||||
return td::Status::Error("Hash mismatch");
|
||||
}
|
||||
Bits256 res;
|
||||
res.as_slice().copy_from(decoded.substr(1, 32));
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace td
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue