2021-11-01 17:21:24 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2021-11-01 17:21:24 +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"
|
2021-11-04 10:46:08 +01:00
|
|
|
#include "td/utils/SharedSlice.h"
|
2021-11-01 17:21:24 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2023-09-29 12:48:22 +02:00
|
|
|
#include "td/utils/StackAllocator.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
2021-11-01 17:21:24 +01:00
|
|
|
#include "td/utils/UInt.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class TlStorerToString {
|
2023-09-29 12:48:22 +02:00
|
|
|
decltype(StackAllocator::alloc(0)) buffer_ = StackAllocator::alloc(1 << 14);
|
|
|
|
StringBuilder sb_ = StringBuilder(buffer_.as_slice(), true);
|
|
|
|
size_t shift_ = 0;
|
2021-11-01 17:21:24 +01:00
|
|
|
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_field_begin(Slice name) {
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_.append_char(shift_, ' ');
|
2023-09-29 13:05:21 +02:00
|
|
|
if (!name.empty()) {
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << name << " = ";
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void store_field_end() {
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_.push_back('\n');
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void store_binary(Slice data) {
|
|
|
|
static const char *hex = "0123456789ABCDEF";
|
|
|
|
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << "{ ";
|
2021-11-01 17:21:24 +01:00
|
|
|
for (auto c : data) {
|
|
|
|
unsigned char byte = c;
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_.push_back(hex[byte >> 4]);
|
|
|
|
sb_.push_back(hex[byte & 15]);
|
|
|
|
sb_.push_back(' ');
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_.push_back('}');
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
TlStorerToString() = default;
|
2023-05-05 12:51:19 +02:00
|
|
|
TlStorerToString(const TlStorerToString &) = delete;
|
|
|
|
TlStorerToString &operator=(const TlStorerToString &) = delete;
|
2023-09-29 12:48:22 +02:00
|
|
|
TlStorerToString(TlStorerToString &&) = delete;
|
|
|
|
TlStorerToString &operator=(TlStorerToString &&) = delete;
|
2021-11-01 17:21:24 +01:00
|
|
|
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_field(Slice name, const string &value) {
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_begin(name);
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_.push_back('"');
|
|
|
|
sb_ << value;
|
|
|
|
sb_.push_back('"');
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_field(Slice name, const SecureString &value) {
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_begin(name);
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << "<secret>";
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_field(Slice name, const T &value) {
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_begin(name);
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << value;
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_bytes_field(Slice name, const SecureString &value) {
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_begin(name);
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << "<secret>";
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class BytesT>
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_bytes_field(Slice name, const BytesT &value) {
|
2021-11-01 17:21:24 +01:00
|
|
|
static const char *hex = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
store_field_begin(name);
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << "bytes [" << value.size() << "] { ";
|
2021-11-01 17:21:24 +01:00
|
|
|
size_t len = min(static_cast<size_t>(64), value.size());
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
int b = value[static_cast<int>(i)] & 0xff;
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_.push_back(hex[b >> 4]);
|
|
|
|
sb_.push_back(hex[b & 15]);
|
|
|
|
sb_.push_back(' ');
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
|
|
|
if (len < value.size()) {
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << "...";
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_.push_back('}');
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ObjectT>
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_object_field(CSlice name, const ObjectT *value) {
|
2021-11-01 17:21:24 +01:00
|
|
|
if (value == nullptr) {
|
2023-09-29 12:48:22 +02:00
|
|
|
store_field(name, Slice("null"));
|
2021-11-01 17:21:24 +01:00
|
|
|
} else {
|
2023-09-29 13:05:21 +02:00
|
|
|
value->store(*this, name.c_str());
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_field(Slice name, const UInt128 &value) {
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_begin(name);
|
|
|
|
store_binary(as_slice(value));
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_field(Slice name, const UInt256 &value) {
|
2021-11-01 17:21:24 +01:00
|
|
|
store_field_begin(name);
|
|
|
|
store_binary(as_slice(value));
|
|
|
|
store_field_end();
|
|
|
|
}
|
|
|
|
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_vector_begin(Slice field_name, size_t vector_size) {
|
2021-11-01 17:45:55 +01:00
|
|
|
store_field_begin(field_name);
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << "vector[" << vector_size << "] {\n";
|
|
|
|
shift_ += 2;
|
2021-11-01 17:45:55 +01:00
|
|
|
}
|
|
|
|
|
2023-09-29 13:05:21 +02:00
|
|
|
void store_class_begin(const char *field_name, Slice class_name) {
|
|
|
|
store_field_begin(Slice(field_name));
|
2023-09-29 12:48:22 +02:00
|
|
|
sb_ << class_name << " {\n";
|
|
|
|
shift_ += 2;
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void store_class_end() {
|
2023-09-29 12:48:22 +02:00
|
|
|
CHECK(shift_ >= 2);
|
|
|
|
shift_ -= 2;
|
|
|
|
sb_.append_char(shift_, ' ');
|
|
|
|
sb_ << "}\n";
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
string move_as_string() {
|
2023-09-29 12:48:22 +02:00
|
|
|
return sb_.as_cslice().str();
|
2021-11-01 17:21:24 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|