diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index 3cbb2af40..dd2543195 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -18,7 +18,6 @@ #include "td/utils/common.h" #include "td/utils/format.h" #include "td/utils/logging.h" -#include "td/utils/misc.h" #include "td/utils/Slice.h" #include "td/utils/StringBuilder.h" #include "td/utils/Variant.h" @@ -404,11 +403,11 @@ class FullRemoteFileLocation { case FileType::Photo: return make_tl_object( photo().id_, photo().access_hash_, BufferSlice(file_reference_), - std::string(1, static_cast(narrow_cast(thumbnail.thumbnail_type)))); + std::string(1, static_cast(static_cast(thumbnail.thumbnail_type)))); case FileType::Thumbnail: return make_tl_object( photo().id_, photo().access_hash_, BufferSlice(file_reference_), - std::string(1, static_cast(narrow_cast(thumbnail.thumbnail_type)))); + std::string(1, static_cast(static_cast(thumbnail.thumbnail_type)))); default: UNREACHABLE(); break; diff --git a/tdutils/td/utils/Enumerator.h b/tdutils/td/utils/Enumerator.h index aad198331..878d9f3cf 100644 --- a/tdutils/td/utils/Enumerator.h +++ b/tdutils/td/utils/Enumerator.h @@ -7,8 +7,8 @@ #pragma once #include "td/utils/common.h" -#include "td/utils/misc.h" +#include #include #include @@ -20,7 +20,8 @@ class Enumerator { using Key = int32; Key add(ValueT v) { - int32 next_id = narrow_cast(arr_.size() + 1); + CHECK(arr_.size() < static_cast(std::numeric_limits::max() - 1)); + int32 next_id = static_cast(arr_.size() + 1); bool was_inserted; decltype(map_.begin()) it; std::tie(it, was_inserted) = map_.emplace(std::move(v), next_id); diff --git a/tdutils/td/utils/buffer.h b/tdutils/td/utils/buffer.h index d540f6099..59b935c5a 100644 --- a/tdutils/td/utils/buffer.h +++ b/tdutils/td/utils/buffer.h @@ -7,8 +7,6 @@ #pragma once #include "td/utils/common.h" -#include "td/utils/logging.h" -#include "td/utils/misc.h" #include "td/utils/port/thread_local.h" #include "td/utils/Slice.h" @@ -605,7 +603,7 @@ class ChainBufferReader { } ChainBufferReader cut_head(size_t offset) TD_WARN_UNUSED_RESULT { - LOG_CHECK(offset <= size()) << offset << " " << size(); + CHECK(offset <= size()); auto it = begin_.clone(); it.advance(offset); return cut_head(std::move(it)); @@ -753,8 +751,8 @@ class BufferBuilder { template void for_each(F &&f) const & { - for (auto &slice : reversed(to_prepend_)) { - f(slice.as_slice()); + for (auto i = to_prepend_.size(); i > 0; i--) { + f(to_prepend_[i - 1].as_slice()); } if (!buffer_writer_.empty()) { f(buffer_writer_.as_slice()); @@ -765,8 +763,8 @@ class BufferBuilder { } template void for_each(F &&f) && { - for (auto &slice : reversed(to_prepend_)) { - f(std::move(slice)); + for (auto i = to_prepend_.size(); i > 0; i--) { + f(std::move(to_prepend_[i - 1])); } if (!buffer_writer_.empty()) { f(buffer_writer_.as_buffer_slice()); diff --git a/tdutils/td/utils/tl_parsers.cpp b/tdutils/td/utils/tl_parsers.cpp index 6d9f3d8a3..b2b4a5923 100644 --- a/tdutils/td/utils/tl_parsers.cpp +++ b/tdutils/td/utils/tl_parsers.cpp @@ -6,10 +6,30 @@ // #include "td/utils/tl_parsers.h" +#include "td/utils/misc.h" + namespace td { alignas(4) const unsigned char TlParser::empty_data[sizeof(UInt256)] = {}; // static zero-initialized +TlParser::TlParser(Slice slice) { + data_len = left_len = slice.size(); + if (is_aligned_pointer<4>(slice.begin())) { + data = slice.ubegin(); + } else { + int32 *buf; + if (data_len <= small_data_array.size() * sizeof(int32)) { + buf = &small_data_array[0]; + } else { + LOG(ERROR) << "Unexpected big unaligned data pointer of length " << slice.size() << " at " << slice.begin(); + data_buf = std::make_unique(1 + data_len / sizeof(int32)); + buf = data_buf.get(); + } + std::memcpy(buf, slice.begin(), slice.size()); + data = reinterpret_cast(buf); + } +} + void TlParser::set_error(const string &error_message) { if (error.empty()) { CHECK(!error_message.empty()); @@ -27,4 +47,11 @@ void TlParser::set_error(const string &error_message) { } } +BufferSlice TlBufferParser::as_buffer_slice(Slice slice) { + if (is_aligned_pointer<4>(slice.data())) { + return parent_->from_slice(slice); + } + return BufferSlice(slice); +} + } // namespace td diff --git a/tdutils/td/utils/tl_parsers.h b/tdutils/td/utils/tl_parsers.h index e730e26ec..1e6c31ee2 100644 --- a/tdutils/td/utils/tl_parsers.h +++ b/tdutils/td/utils/tl_parsers.h @@ -10,7 +10,6 @@ #include "td/utils/common.h" #include "td/utils/format.h" #include "td/utils/logging.h" -#include "td/utils/misc.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" #include "td/utils/UInt.h" @@ -38,23 +37,7 @@ class TlParser { alignas(4) static const unsigned char empty_data[sizeof(UInt256)]; public: - explicit TlParser(Slice slice) { - data_len = left_len = slice.size(); - if (is_aligned_pointer<4>(slice.begin())) { - data = slice.ubegin(); - } else { - int32 *buf; - if (data_len <= small_data_array.size() * sizeof(int32)) { - buf = &small_data_array[0]; - } else { - LOG(ERROR) << "Unexpected big unaligned data pointer of length " << slice.size() << " at " << slice.begin(); - data_buf = std::make_unique(1 + data_len / sizeof(int32)); - buf = data_buf.get(); - } - std::memcpy(buf, slice.begin(), slice.size()); - data = reinterpret_cast(buf); - } - } + explicit TlParser(Slice slice); TlParser(const TlParser &other) = delete; TlParser &operator=(const TlParser &other) = delete; @@ -143,15 +126,15 @@ class TlParser { T fetch_string() { check_len(sizeof(int32)); size_t result_len = *data; - const char *result_begin; + const unsigned char *result_begin; size_t result_aligned_len; if (result_len < 254) { - result_begin = reinterpret_cast(data + 1); + result_begin = data + 1; result_aligned_len = (result_len >> 2) << 2; data += sizeof(int32); } else if (result_len == 254) { result_len = data[1] + (data[2] << 8) + (data[3] << 16); - result_begin = reinterpret_cast(data + 4); + result_begin = data + 4; result_aligned_len = ((result_len + 3) >> 2) << 2; data += sizeof(int32); } else { @@ -165,7 +148,7 @@ class TlParser { return T(); } result_len = static_cast(result_len_uint64); - result_begin = reinterpret_cast(data + 8); + result_begin = data + 8; result_aligned_len = ((result_len + 3) >> 2) << 2; data += sizeof(int64); } @@ -174,7 +157,7 @@ class TlParser { return T(); } data += result_aligned_len; - return T(result_begin, result_len); + return T(reinterpret_cast(result_begin), result_len); } template @@ -204,6 +187,7 @@ class TlBufferParser : public TlParser { public: explicit TlBufferParser(const BufferSlice *buffer_slice) : TlParser(buffer_slice->as_slice()), parent_(buffer_slice) { } + template T fetch_string() { auto result = TlParser::fetch_string(); @@ -230,6 +214,7 @@ class TlBufferParser : public TlParser { return T(); } + template T fetch_string_raw(const size_t size) { return TlParser::fetch_string_raw(size); @@ -238,12 +223,7 @@ class TlBufferParser : public TlParser { private: const BufferSlice *parent_; - BufferSlice as_buffer_slice(Slice slice) { - if (is_aligned_pointer<4>(slice.data())) { - return parent_->from_slice(slice); - } - return BufferSlice(slice); - } + BufferSlice as_buffer_slice(Slice slice); }; template <> diff --git a/tdutils/td/utils/tl_storers.h b/tdutils/td/utils/tl_storers.h index f34c8f66d..096b53927 100644 --- a/tdutils/td/utils/tl_storers.h +++ b/tdutils/td/utils/tl_storers.h @@ -8,7 +8,6 @@ #include "td/utils/common.h" #include "td/utils/logging.h" -#include "td/utils/misc.h" #include "td/utils/Slice.h" #include "td/utils/StorerBase.h" #include "td/utils/UInt.h" @@ -22,7 +21,6 @@ class TlStorerUnsafe { public: explicit TlStorerUnsafe(unsigned char *buf) : buf_(buf) { - LOG_CHECK(is_aligned_pointer<4>(buf_)) << buf_; } TlStorerUnsafe(const TlStorerUnsafe &other) = delete;