2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/misc.h"
|
2019-08-12 13:45:57 +02:00
|
|
|
#include "td/utils/SharedSlice.h"
|
2019-08-13 22:52:54 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/tl_parsers.h"
|
|
|
|
#include "td/utils/tl_storers.h"
|
2019-06-19 01:48:34 +02:00
|
|
|
#include "td/utils/Variant.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <unordered_set>
|
2019-09-18 01:14:24 +02:00
|
|
|
#include <utility>
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-07-19 21:00:47 +02:00
|
|
|
#define BEGIN_STORE_FLAGS() \
|
|
|
|
do { \
|
|
|
|
::td::uint32 flags_store = 0; \
|
|
|
|
::td::uint32 bit_offset_store = 0
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-03-01 19:39:24 +01:00
|
|
|
#define STORE_FLAG(flag) \
|
|
|
|
static_assert(std::is_same<decltype(flag), bool>::value, "flag must be bool"); \
|
|
|
|
flags_store |= (flag) << bit_offset_store; \
|
2018-12-31 20:04:05 +01:00
|
|
|
bit_offset_store++
|
|
|
|
|
2020-07-19 21:00:47 +02:00
|
|
|
#define END_STORE_FLAGS() \
|
|
|
|
CHECK(bit_offset_store < 31); \
|
|
|
|
::td::store(flags_store, storer); \
|
|
|
|
} \
|
2018-11-09 13:38:41 +01:00
|
|
|
while (false)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-07-19 21:00:47 +02:00
|
|
|
#define BEGIN_PARSE_FLAGS() \
|
|
|
|
do { \
|
|
|
|
::td::uint32 flags_parse; \
|
|
|
|
::td::uint32 bit_offset_parse = 0; \
|
|
|
|
::td::parse(flags_parse, parser)
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-03-01 19:39:24 +01:00
|
|
|
#define PARSE_FLAG(flag) \
|
|
|
|
static_assert(std::is_same<decltype(flag), bool>::value, "flag must be bool"); \
|
|
|
|
flag = ((flags_parse >> bit_offset_parse) & 1) != 0; \
|
2018-12-31 20:04:05 +01:00
|
|
|
bit_offset_parse++
|
|
|
|
|
2019-08-01 02:40:28 +02:00
|
|
|
#define END_PARSE_FLAGS() \
|
|
|
|
CHECK(bit_offset_parse < 31); \
|
|
|
|
if ((flags_parse & ~((1 << bit_offset_parse) - 1)) != 0) { \
|
|
|
|
parser.set_error(PSTRING() << "Invalid flags " << flags_parse << " left, current bit is " << bit_offset_parse); \
|
|
|
|
} \
|
|
|
|
} \
|
2019-07-31 12:45:32 +02:00
|
|
|
while (false)
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2019-01-19 23:59:37 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(bool x, StorerT &storer) {
|
|
|
|
storer.store_binary(static_cast<int32>(x));
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(bool &x, ParserT &parser) {
|
|
|
|
x = parser.fetch_int() != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(int32 x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(int32 &x, ParserT &parser) {
|
|
|
|
x = parser.fetch_int();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(uint32 x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(uint32 &x, ParserT &parser) {
|
|
|
|
x = static_cast<uint32>(parser.fetch_int());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(int64 x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(int64 &x, ParserT &parser) {
|
|
|
|
x = parser.fetch_long();
|
|
|
|
}
|
|
|
|
template <class StorerT>
|
|
|
|
void store(uint64 x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(uint64 &x, ParserT &parser) {
|
|
|
|
x = static_cast<uint64>(parser.fetch_long());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(double x, StorerT &storer) {
|
|
|
|
storer.store_binary(x);
|
|
|
|
}
|
|
|
|
template <class ParserT>
|
|
|
|
void parse(double &x, ParserT &parser) {
|
|
|
|
x = parser.fetch_double();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class StorerT>
|
|
|
|
void store(Slice x, StorerT &storer) {
|
|
|
|
storer.store_string(x);
|
|
|
|
}
|
|
|
|
template <class StorerT>
|
|
|
|
void store(const string &x, StorerT &storer) {
|
|
|
|
storer.store_string(x);
|
|
|
|
}
|
2019-08-12 13:45:57 +02:00
|
|
|
template <class StorerT>
|
|
|
|
void store(const SecureString &x, StorerT &storer) {
|
|
|
|
storer.store_string(x.as_slice());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class ParserT>
|
|
|
|
void parse(string &x, ParserT &parser) {
|
|
|
|
x = parser.template fetch_string<string>();
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
template <class ParserT>
|
|
|
|
void parse(SecureString &x, ParserT &parser) {
|
|
|
|
x = parser.template fetch_string<SecureString>();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T, class StorerT>
|
|
|
|
void store(const vector<T> &vec, StorerT &storer) {
|
|
|
|
storer.store_binary(narrow_cast<int32>(vec.size()));
|
|
|
|
for (auto &val : vec) {
|
|
|
|
store(val, storer);
|
|
|
|
}
|
|
|
|
}
|
2020-05-25 01:40:00 +02:00
|
|
|
template <class T, class StorerT>
|
|
|
|
void store(const vector<T *> &vec, StorerT &storer) {
|
|
|
|
storer.store_binary(narrow_cast<int32>(vec.size()));
|
|
|
|
for (auto &val : vec) {
|
|
|
|
store(*val, storer);
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T, class ParserT>
|
|
|
|
void parse(vector<T> &vec, ParserT &parser) {
|
|
|
|
uint32 size = parser.fetch_int();
|
|
|
|
if (parser.get_left_len() < size) {
|
|
|
|
parser.set_error("Wrong vector length");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vec = vector<T>(size);
|
|
|
|
for (auto &val : vec) {
|
|
|
|
parse(val, parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:42:38 +02:00
|
|
|
template <class T, class StorerT>
|
|
|
|
void store(const unique_ptr<T> &ptr, StorerT &storer) {
|
|
|
|
CHECK(ptr != nullptr);
|
|
|
|
store(*ptr, storer);
|
|
|
|
}
|
|
|
|
template <class T, class ParserT>
|
|
|
|
void parse(unique_ptr<T> &ptr, ParserT &parser) {
|
|
|
|
CHECK(ptr == nullptr);
|
|
|
|
ptr = make_unique<T>();
|
|
|
|
parse(*ptr, parser);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class Key, class Hash, class KeyEqual, class Allocator, class StorerT>
|
|
|
|
void store(const std::unordered_set<Key, Hash, KeyEqual, Allocator> &s, StorerT &storer) {
|
|
|
|
storer.store_binary(narrow_cast<int32>(s.size()));
|
|
|
|
for (auto &val : s) {
|
|
|
|
store(val, storer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
template <class Key, class Hash, class KeyEqual, class Allocator, class ParserT>
|
|
|
|
void parse(std::unordered_set<Key, Hash, KeyEqual, Allocator> &s, ParserT &parser) {
|
|
|
|
uint32 size = parser.fetch_int();
|
|
|
|
if (parser.get_left_len() < size) {
|
|
|
|
parser.set_error("Wrong set length");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s.clear();
|
|
|
|
for (uint32 i = 0; i < size; i++) {
|
2018-11-11 14:35:51 +01:00
|
|
|
Key val;
|
2018-12-31 20:04:05 +01:00
|
|
|
parse(val, parser);
|
|
|
|
s.insert(std::move(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 01:14:24 +02:00
|
|
|
template <class U, class V, class StorerT>
|
|
|
|
void store(const std::pair<U, V> &pair, StorerT &storer) {
|
|
|
|
store(pair.first, storer);
|
|
|
|
store(pair.second, storer);
|
|
|
|
}
|
|
|
|
template <class U, class V, class ParserT>
|
|
|
|
void parse(std::pair<U, V> &pair, ParserT &parser) {
|
|
|
|
parse(pair.first, parser);
|
|
|
|
parse(pair.second, parser);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T, class StorerT>
|
|
|
|
std::enable_if_t<std::is_enum<T>::value> store(const T &val, StorerT &storer) {
|
|
|
|
store(static_cast<int32>(val), storer);
|
|
|
|
}
|
|
|
|
template <class T, class ParserT>
|
|
|
|
std::enable_if_t<std::is_enum<T>::value> parse(T &val, ParserT &parser) {
|
|
|
|
int32 result;
|
|
|
|
parse(result, parser);
|
|
|
|
val = static_cast<T>(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class StorerT>
|
|
|
|
std::enable_if_t<!std::is_enum<T>::value> store(const T &val, StorerT &storer) {
|
|
|
|
val.store(storer);
|
|
|
|
}
|
|
|
|
template <class T, class ParserT>
|
|
|
|
std::enable_if_t<!std::is_enum<T>::value> parse(T &val, ParserT &parser) {
|
|
|
|
val.parse(parser);
|
|
|
|
}
|
|
|
|
|
2019-06-19 01:48:34 +02:00
|
|
|
template <class... Types, class StorerT>
|
|
|
|
void store(const Variant<Types...> &variant, StorerT &storer) {
|
|
|
|
store(variant.get_offset(), storer);
|
|
|
|
variant.visit([&storer](auto &&value) {
|
|
|
|
using td::store;
|
|
|
|
store(value, storer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
template <class... Types, class ParserT>
|
|
|
|
void parse(Variant<Types...> &variant, ParserT &parser) {
|
|
|
|
auto type_offset = parser.fetch_int();
|
|
|
|
if (type_offset < 0 || type_offset >= static_cast<int32>(sizeof...(Types))) {
|
|
|
|
return parser.set_error("Invalid type");
|
|
|
|
}
|
|
|
|
variant.for_each([type_offset, &parser, &variant](int offset, auto *ptr) {
|
|
|
|
using T = std::decay_t<decltype(*ptr)>;
|
|
|
|
if (offset == type_offset) {
|
|
|
|
variant = T();
|
2019-06-20 18:01:18 +02:00
|
|
|
parse(variant.template get<T>(), parser);
|
2019-06-19 01:48:34 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
string serialize(const T &object) {
|
|
|
|
TlStorerCalcLength calc_length;
|
|
|
|
store(object, calc_length);
|
|
|
|
size_t length = calc_length.get_length();
|
|
|
|
|
|
|
|
string key(length, '\0');
|
|
|
|
if (!is_aligned_pointer<4>(key.data())) {
|
|
|
|
auto ptr = StackAllocator::alloc(length);
|
|
|
|
MutableSlice data = ptr.as_slice();
|
2018-06-25 01:58:43 +02:00
|
|
|
TlStorerUnsafe storer(data.ubegin());
|
2018-12-31 20:04:05 +01:00
|
|
|
store(object, storer);
|
2018-07-06 22:33:11 +02:00
|
|
|
CHECK(storer.get_buf() == data.uend());
|
2018-12-31 20:04:05 +01:00
|
|
|
key.assign(data.begin(), data.size());
|
|
|
|
} else {
|
|
|
|
MutableSlice data = key;
|
2018-06-25 01:58:43 +02:00
|
|
|
TlStorerUnsafe storer(data.ubegin());
|
2018-12-31 20:04:05 +01:00
|
|
|
store(object, storer);
|
2018-07-06 22:33:11 +02:00
|
|
|
CHECK(storer.get_buf() == data.uend());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:45:57 +02:00
|
|
|
template <class T>
|
|
|
|
SecureString serialize_secure(const T &object) {
|
|
|
|
TlStorerCalcLength calc_length;
|
|
|
|
store(object, calc_length);
|
|
|
|
size_t length = calc_length.get_length();
|
|
|
|
|
|
|
|
SecureString key(length, '\0');
|
|
|
|
CHECK(is_aligned_pointer<4>(key.data()));
|
|
|
|
MutableSlice data = key.as_mutable_slice();
|
|
|
|
TlStorerUnsafe storer(data.ubegin());
|
|
|
|
store(object, storer);
|
|
|
|
CHECK(storer.get_buf() == data.uend());
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
TD_WARN_UNUSED_RESULT Status unserialize(T &object, Slice data) {
|
|
|
|
TlParser parser(data);
|
|
|
|
parse(object, parser);
|
|
|
|
parser.fetch_end();
|
|
|
|
return parser.get_status();
|
|
|
|
}
|
2019-01-19 23:59:37 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|