2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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/buffer.h"
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
2018-12-19 22:18:53 +01:00
|
|
|
#include "td/utils/UInt.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/utf8.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstring>
|
|
|
|
#include <limits>
|
2018-09-27 03:19:03 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class TlParser {
|
|
|
|
const unsigned char *data = nullptr;
|
|
|
|
size_t data_len = 0;
|
|
|
|
size_t left_len = 0;
|
|
|
|
size_t error_pos = std::numeric_limits<size_t>::max();
|
|
|
|
std::string error;
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
std::unique_ptr<int32[]> data_buf;
|
2018-12-31 20:04:05 +01:00
|
|
|
static constexpr size_t SMALL_DATA_ARRAY_SIZE = 6;
|
|
|
|
std::array<int32, SMALL_DATA_ARRAY_SIZE> small_data_array;
|
|
|
|
|
2018-02-12 18:21:29 +01:00
|
|
|
alignas(4) static const unsigned char empty_data[sizeof(UInt256)];
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2019-11-26 18:53:50 +01:00
|
|
|
explicit TlParser(Slice slice);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
TlParser(const TlParser &other) = delete;
|
|
|
|
TlParser &operator=(const TlParser &other) = delete;
|
|
|
|
|
|
|
|
void set_error(const string &error_message);
|
|
|
|
|
|
|
|
const char *get_error() const {
|
|
|
|
if (error.empty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return error.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_error_pos() const {
|
|
|
|
return error_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status get_status() const {
|
|
|
|
if (error.empty()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
return Status::Error(PSLICE() << error << " at " << error_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_len(const size_t len) {
|
|
|
|
if (unlikely(left_len < len)) {
|
|
|
|
set_error("Not enough data to read");
|
|
|
|
} else {
|
|
|
|
left_len -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
bool can_prefetch_int() const {
|
|
|
|
return get_left_len() >= sizeof(int32);
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 prefetch_int_unsafe() const {
|
|
|
|
int32 result;
|
|
|
|
std::memcpy(&result, data, sizeof(int32));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
int32 fetch_int_unsafe() {
|
2018-08-13 19:15:09 +02:00
|
|
|
int32 result;
|
2018-12-19 22:44:15 +01:00
|
|
|
std::memcpy(&result, data, sizeof(int32));
|
2018-12-31 20:04:05 +01:00
|
|
|
data += sizeof(int32);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 fetch_int() {
|
|
|
|
check_len(sizeof(int32));
|
|
|
|
return fetch_int_unsafe();
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 fetch_long_unsafe() {
|
|
|
|
int64 result;
|
2018-12-19 22:44:15 +01:00
|
|
|
std::memcpy(&result, data, sizeof(int64));
|
2018-12-31 20:04:05 +01:00
|
|
|
data += sizeof(int64);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 fetch_long() {
|
|
|
|
check_len(sizeof(int64));
|
|
|
|
return fetch_long_unsafe();
|
|
|
|
}
|
|
|
|
|
|
|
|
double fetch_double_unsafe() {
|
|
|
|
double result;
|
2018-12-19 22:44:15 +01:00
|
|
|
std::memcpy(&result, data, sizeof(double));
|
2018-12-31 20:04:05 +01:00
|
|
|
data += sizeof(double);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
double fetch_double() {
|
|
|
|
check_len(sizeof(double));
|
|
|
|
return fetch_double_unsafe();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T fetch_binary_unsafe() {
|
|
|
|
T result;
|
2018-12-19 22:44:15 +01:00
|
|
|
std::memcpy(&result, data, sizeof(T));
|
2018-12-31 20:04:05 +01:00
|
|
|
data += sizeof(T);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T fetch_binary() {
|
|
|
|
static_assert(sizeof(T) <= sizeof(empty_data), "too big fetch_binary");
|
2018-08-13 19:15:09 +02:00
|
|
|
//static_assert(sizeof(T) % sizeof(int32) == 0, "wrong call to fetch_binary");
|
2018-12-31 20:04:05 +01:00
|
|
|
check_len(sizeof(T));
|
|
|
|
return fetch_binary_unsafe<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T fetch_string() {
|
|
|
|
check_len(sizeof(int32));
|
|
|
|
size_t result_len = *data;
|
2019-11-26 18:53:50 +01:00
|
|
|
const unsigned char *result_begin;
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t result_aligned_len;
|
|
|
|
if (result_len < 254) {
|
2019-11-26 18:53:50 +01:00
|
|
|
result_begin = data + 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
result_aligned_len = (result_len >> 2) << 2;
|
2019-08-12 13:45:57 +02:00
|
|
|
data += sizeof(int32);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (result_len == 254) {
|
|
|
|
result_len = data[1] + (data[2] << 8) + (data[3] << 16);
|
2019-11-26 18:53:50 +01:00
|
|
|
result_begin = data + 4;
|
2018-12-31 20:04:05 +01:00
|
|
|
result_aligned_len = ((result_len + 3) >> 2) << 2;
|
2019-08-12 13:45:57 +02:00
|
|
|
data += sizeof(int32);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-08-12 13:45:57 +02:00
|
|
|
check_len(sizeof(int32));
|
2019-09-05 17:32:45 +02:00
|
|
|
auto result_len_uint64 = static_cast<uint64>(data[1]) + (static_cast<uint64>(data[2]) << 8) +
|
|
|
|
(static_cast<uint64>(data[3]) << 16) + (static_cast<uint64>(data[4]) << 24) +
|
2019-08-13 22:52:54 +02:00
|
|
|
(static_cast<uint64>(data[5]) << 32) + (static_cast<uint64>(data[6]) << 40) +
|
|
|
|
(static_cast<uint64>(data[7]) << 48);
|
|
|
|
if (result_len_uint64 > std::numeric_limits<size_t>::max() - 3) {
|
2019-08-12 13:45:57 +02:00
|
|
|
set_error("Too big string found");
|
|
|
|
return T();
|
|
|
|
}
|
2019-08-13 22:52:54 +02:00
|
|
|
result_len = static_cast<size_t>(result_len_uint64);
|
2019-11-26 18:53:50 +01:00
|
|
|
result_begin = data + 8;
|
2019-08-12 13:45:57 +02:00
|
|
|
result_aligned_len = ((result_len + 3) >> 2) << 2;
|
|
|
|
data += sizeof(int64);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
check_len(result_aligned_len);
|
2019-08-12 13:45:57 +02:00
|
|
|
if (!error.empty()) {
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
data += result_aligned_len;
|
2019-11-26 18:53:50 +01:00
|
|
|
return T(reinterpret_cast<const char *>(result_begin), result_len);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
T fetch_string_raw(const size_t size) {
|
2018-08-13 19:15:09 +02:00
|
|
|
//CHECK(size % sizeof(int32) == 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
check_len(size);
|
2019-08-12 13:45:57 +02:00
|
|
|
if (!error.empty()) {
|
|
|
|
return T();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
const char *result = reinterpret_cast<const char *>(data);
|
|
|
|
data += size;
|
|
|
|
return T(result, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fetch_end() {
|
|
|
|
if (left_len) {
|
|
|
|
set_error("Too much data to fetch");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t get_left_len() const {
|
|
|
|
return left_len;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TlBufferParser : public TlParser {
|
|
|
|
public:
|
|
|
|
explicit TlBufferParser(const BufferSlice *buffer_slice) : TlParser(buffer_slice->as_slice()), parent_(buffer_slice) {
|
|
|
|
}
|
2019-11-26 18:53:50 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
T fetch_string() {
|
|
|
|
auto result = TlParser::fetch_string<T>();
|
|
|
|
for (auto &c : result) {
|
|
|
|
if (c == '\0') {
|
|
|
|
c = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (check_utf8(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
CHECK(!result.empty());
|
|
|
|
LOG(WARNING) << "Wrong UTF-8 string [[" << result << "]] in " << format::as_hex_dump<4>(parent_->as_slice());
|
|
|
|
|
|
|
|
// trying to remove last character
|
|
|
|
size_t new_size = result.size() - 1;
|
|
|
|
while (new_size != 0 && !is_utf8_character_first_code_unit(static_cast<unsigned char>(result[new_size]))) {
|
|
|
|
new_size--;
|
|
|
|
}
|
|
|
|
result.resize(new_size);
|
|
|
|
if (check_utf8(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return T();
|
|
|
|
}
|
2019-11-26 18:53:50 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
template <class T>
|
|
|
|
T fetch_string_raw(const size_t size) {
|
|
|
|
return TlParser::fetch_string_raw<T>(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const BufferSlice *parent_;
|
|
|
|
|
2019-11-26 18:53:50 +01:00
|
|
|
BufferSlice as_buffer_slice(Slice slice);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline BufferSlice TlBufferParser::fetch_string<BufferSlice>() {
|
|
|
|
return as_buffer_slice(TlParser::fetch_string<Slice>());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline BufferSlice TlBufferParser::fetch_string_raw<BufferSlice>(const size_t size) {
|
|
|
|
return as_buffer_slice(TlParser::fetch_string_raw<Slice>(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|