2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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
|
|
|
|
|
2018-12-12 17:27:58 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.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) {
|
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-12-19 22:44:15 +01:00
|
|
|
std::memcpy(buf_, &x, sizeof(T));
|
2018-06-25 01:58:43 +02:00
|
|
|
buf_ += sizeof(T);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void store_int(int32 x) {
|
2018-08-13 19:15:09 +02:00
|
|
|
store_binary<int32>(x);
|
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
|
|
|
}
|
2021-11-01 17:21:24 +01:00
|
|
|
|
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);
|
2019-08-23 17:56:01 +02:00
|
|
|
} else if (static_cast<uint64>(len) < (static_cast<uint64>(1) << 32)) {
|
2019-08-12 13:45:57 +02:00
|
|
|
*buf_++ = static_cast<unsigned char>(255);
|
|
|
|
*buf_++ = static_cast<unsigned char>(len & 255);
|
|
|
|
*buf_++ = static_cast<unsigned char>((len >> 8) & 255);
|
|
|
|
*buf_++ = static_cast<unsigned char>((len >> 16) & 255);
|
|
|
|
*buf_++ = static_cast<unsigned char>((len >> 24) & 255);
|
|
|
|
*buf_++ = static_cast<unsigned char>(0);
|
|
|
|
*buf_++ = static_cast<unsigned char>(0);
|
|
|
|
*buf_++ = static_cast<unsigned char>(0);
|
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;
|
2019-08-12 13:45:57 +02:00
|
|
|
// fallthrough
|
2018-12-31 20:04:05 +01:00
|
|
|
case 2:
|
2018-06-25 01:58:43 +02:00
|
|
|
*buf_++ = 0;
|
2019-08-13 22:52:54 +02:00
|
|
|
// fallthrough
|
2018-12-31 20:04:05 +01:00
|
|
|
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;
|
2019-08-12 13:45:57 +02:00
|
|
|
} else if (add < (1 << 24)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
add += 4;
|
2019-08-12 13:45:57 +02:00
|
|
|
} else {
|
|
|
|
add += 8;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
add = (add + 3) & -4;
|
|
|
|
length += add;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_length() const {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-07-06 22:33:11 +02:00
|
|
|
template <class T>
|
|
|
|
size_t tl_store_unsafe(const T &data, unsigned char *dst) TD_WARN_UNUSED_RESULT;
|
|
|
|
|
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
|