2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-01-02 14:42:31 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
|
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/int_types.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/StorerBase.h"
|
|
|
|
|
2018-02-03 13:58:18 +01:00
|
|
|
#include <cstring>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class TlStorerUnsafe {
|
2018-06-25 01:58:43 +02:00
|
|
|
unsigned char *buf_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2018-06-25 01:58:43 +02:00
|
|
|
explicit TlStorerUnsafe(unsigned char *buf) : buf_(buf) {
|
|
|
|
CHECK(is_aligned_pointer<4>(buf_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TlStorerUnsafe(const TlStorerUnsafe &other) = delete;
|
|
|
|
TlStorerUnsafe &operator=(const TlStorerUnsafe &other) = delete;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void store_binary(const T &x) {
|
2018-06-25 01:58:43 +02:00
|
|
|
std::memcpy(buf_, reinterpret_cast<const unsigned char *>(&x), sizeof(T));
|
|
|
|
buf_ += sizeof(T);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void store_int(int32 x) {
|
2018-06-25 01:58:43 +02:00
|
|
|
*reinterpret_cast<int32 *>(buf_) = x;
|
|
|
|
buf_ += sizeof(int32);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void store_long(int64 x) {
|
|
|
|
store_binary<int64>(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_slice(Slice slice) {
|
2018-06-25 01:58:43 +02:00
|
|
|
std::memcpy(buf_, slice.begin(), slice.size());
|
|
|
|
buf_ += slice.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void store_storer(const Storer &storer) {
|
2018-06-25 01:58:43 +02:00
|
|
|
size_t size = storer.store(buf_);
|
|
|
|
buf_ += size;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void store_string(const T &str) {
|
|
|
|
size_t len = str.size();
|
|
|
|
if (len < 254) {
|
2018-06-25 01:58:43 +02:00
|
|
|
*buf_++ = static_cast<unsigned char>(len);
|
2018-12-31 20:04:05 +01:00
|
|
|
len++;
|
|
|
|
} else if (len < (1 << 24)) {
|
2018-06-25 01:58:43 +02:00
|
|
|
*buf_++ = static_cast<unsigned char>(254);
|
|
|
|
*buf_++ = static_cast<unsigned char>(len & 255);
|
|
|
|
*buf_++ = static_cast<unsigned char>((len >> 8) & 255);
|
|
|
|
*buf_++ = static_cast<unsigned char>(len >> 16);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
LOG(FATAL) << "String size " << len << " is too big to be stored";
|
|
|
|
}
|
2018-06-25 01:58:43 +02:00
|
|
|
std::memcpy(buf_, str.data(), str.size());
|
|
|
|
buf_ += str.size();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
switch (len & 3) {
|
|
|
|
case 1:
|
2018-06-25 01:58:43 +02:00
|
|
|
*buf_++ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
// fallthrough
|
|
|
|
case 2:
|
2018-06-25 01:58:43 +02:00
|
|
|
*buf_++ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
// fallthrough
|
|
|
|
case 3:
|
2018-06-25 01:58:43 +02:00
|
|
|
*buf_++ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 01:58:43 +02:00
|
|
|
unsigned char *get_buf() const {
|
|
|
|
return buf_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlStorerCalcLength {
|
|
|
|
size_t length = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
TlStorerCalcLength() = default;
|
|
|
|
TlStorerCalcLength(const TlStorerCalcLength &other) = delete;
|
|
|
|
TlStorerCalcLength &operator=(const TlStorerCalcLength &other) = delete;
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void store_binary(const T &x) {
|
|
|
|
length += sizeof(T);
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_int(int32 x) {
|
|
|
|
store_binary<int32>(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_long(int64 x) {
|
|
|
|
store_binary<int64>(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_slice(Slice slice) {
|
|
|
|
length += slice.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_storer(const Storer &storer) {
|
|
|
|
length += storer.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void store_string(const T &str) {
|
|
|
|
size_t add = str.size();
|
|
|
|
if (add < 254) {
|
|
|
|
add += 1;
|
|
|
|
} else {
|
|
|
|
add += 4;
|
|
|
|
}
|
|
|
|
add = (add + 3) & -4;
|
|
|
|
length += add;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_length() const {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlStorerToString {
|
|
|
|
std::string result;
|
|
|
|
int shift = 0;
|
|
|
|
|
|
|
|
void store_field_begin(const char *name) {
|
|
|
|
for (int i = 0; i < shift; i++) {
|
|
|
|
result += ' ';
|
|
|
|
}
|
|
|
|
if (name && name[0]) {
|
|
|
|
result += name;
|
|
|
|
result += " = ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field_end() {
|
|
|
|
result += "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_long(int64 value) {
|
|
|
|
result += (PSLICE() << value).c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_binary(Slice data) {
|
|
|
|
static const char *hex = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
result.append("{ ");
|
|
|
|
for (auto c : data) {
|
|
|
|
unsigned char byte = c;
|
|
|
|
result += hex[byte >> 4];
|
|
|
|
result += hex[byte & 15];
|
|
|
|
result += ' ';
|
|
|
|
}
|
|
|
|
result.append("}");
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
TlStorerToString() = default;
|
|
|
|
TlStorerToString(const TlStorerToString &other) = delete;
|
|
|
|
TlStorerToString &operator=(const TlStorerToString &other) = delete;
|
|
|
|
|
|
|
|
void store_field(const char *name, bool value) {
|
|
|
|
store_field_begin(name);
|
|
|
|
result += (value ? "true" : "false");
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field(const char *name, int32 value) {
|
|
|
|
store_field(name, static_cast<int64>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field(const char *name, int64 value) {
|
|
|
|
store_field_begin(name);
|
|
|
|
store_long(value);
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field(const char *name, double value) {
|
|
|
|
store_field_begin(name);
|
|
|
|
result += (PSLICE() << value).c_str();
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field(const char *name, const char *value) {
|
|
|
|
store_field_begin(name);
|
|
|
|
result += value;
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field(const char *name, const string &value) {
|
|
|
|
store_field_begin(name);
|
|
|
|
result += '"';
|
|
|
|
result.append(value.data(), value.size());
|
|
|
|
result += '"';
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void store_field(const char *name, const T &value) {
|
|
|
|
store_field_begin(name);
|
|
|
|
result.append(value.data(), value.size());
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BytesT>
|
|
|
|
void store_bytes_field(const char *name, const BytesT &value) {
|
|
|
|
static const char *hex = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
store_field_begin(name);
|
2018-04-26 23:49:23 +02:00
|
|
|
result.append("bytes [");
|
|
|
|
store_long(static_cast<int64>(value.size()));
|
|
|
|
result.append("] { ");
|
2018-12-31 20:04:05 +01:00
|
|
|
for (size_t i = 0; i < value.size(); i++) {
|
|
|
|
int b = value[static_cast<int>(i)] & 0xff;
|
|
|
|
result += hex[b >> 4];
|
|
|
|
result += hex[b & 15];
|
|
|
|
result += ' ';
|
|
|
|
}
|
|
|
|
result.append("}");
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field(const char *name, const UInt128 &value) {
|
|
|
|
store_field_begin(name);
|
|
|
|
store_binary(Slice(reinterpret_cast<const unsigned char *>(&value), sizeof(value)));
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field(const char *name, const UInt256 &value) {
|
|
|
|
store_field_begin(name);
|
|
|
|
store_binary(Slice(reinterpret_cast<const unsigned char *>(&value), sizeof(value)));
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_class_begin(const char *field_name, const char *class_name) {
|
|
|
|
store_field_begin(field_name);
|
|
|
|
result += class_name;
|
|
|
|
result += " {\n";
|
|
|
|
shift += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_class_end() {
|
|
|
|
shift -= 2;
|
|
|
|
for (int i = 0; i < shift; i++) {
|
|
|
|
result += ' ';
|
|
|
|
}
|
|
|
|
result += "}\n";
|
|
|
|
CHECK(shift >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str() const {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
size_t tl_calc_length(const T &data) {
|
|
|
|
TlStorerCalcLength storer_calc_length;
|
|
|
|
data.store(storer_calc_length);
|
|
|
|
return storer_calc_length.get_length();
|
|
|
|
}
|
|
|
|
|
2018-06-25 01:58:43 +02:00
|
|
|
template <class T>
|
|
|
|
size_t tl_store_unsafe(const T &data, unsigned char *dst) {
|
|
|
|
TlStorerUnsafe storer_unsafe(dst);
|
2018-12-31 20:04:05 +01:00
|
|
|
data.store(storer_unsafe);
|
2018-06-25 01:58:43 +02:00
|
|
|
return static_cast<size_t>(storer_unsafe.get_buf() - dst);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|