diff --git a/td/mtproto/TcpTransport.cpp b/td/mtproto/TcpTransport.cpp index bf09ea6e..dd1da5be 100644 --- a/td/mtproto/TcpTransport.cpp +++ b/td/mtproto/TcpTransport.cpp @@ -149,12 +149,12 @@ void ObfuscatedTransport::init(ChainBufferReader *input, ChainBufferWriter *outp if (as(header.data()) == 0xef) { continue; } - auto first_int = as(header.data()); + uint32 first_int = as(header.data()); if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f || first_int == 0xdddddddd || first_int == 0xeeeeeeee) { continue; } - auto second_int = as(header.data() + sizeof(uint32)); + uint32 second_int = as(header.data() + sizeof(uint32)); if (second_int == 0) { continue; } diff --git a/td/mtproto/Transport.cpp b/td/mtproto/Transport.cpp index 58915142..5a460d7c 100644 --- a/td/mtproto/Transport.cpp +++ b/td/mtproto/Transport.cpp @@ -296,7 +296,7 @@ Result Transport::read(MutableSlice message, const AuthKe return Status::Error(PSLICE() << "Invalid mtproto message: smaller than 4 bytes [size=" << message.size() << "]"); } - auto code = as(message.begin()); + int32 code = as(message.begin()); if (code == 0) { return ReadResult::make_nop(); } else if (code == -1 && message.size() >= 8) { diff --git a/td/telegram/SecretChatActor.cpp b/td/telegram/SecretChatActor.cpp index e971f38f..b0c225f3 100644 --- a/td/telegram/SecretChatActor.cpp +++ b/td/telegram/SecretChatActor.cpp @@ -865,7 +865,7 @@ Result> SecretChatActor::decrypt(BufferSl UNREACHABLE(); } - auto len = as(data.begin()); + int32 len = as(data.begin()); data = data.substr(4, len); if (!is_aligned_pointer<4>(data.data())) { return std::make_tuple(auth_key_id, BufferSlice(data), mtproto_version); diff --git a/td/telegram/SecureStorage.cpp b/td/telegram/SecureStorage.cpp index cc962156..6df6d5c6 100644 --- a/td/telegram/SecureStorage.cpp +++ b/td/telegram/SecureStorage.cpp @@ -184,7 +184,7 @@ Result Secret::create(Slice secret) { UInt256 secret_sha256; sha256(secret, ::td::as_slice(secret_sha256)); - auto hash = as(secret_sha256.raw); + int64 hash = as(secret_sha256.raw); return Secret{res, hash}; } diff --git a/td/telegram/files/FileDownloader.cpp b/td/telegram/files/FileDownloader.cpp index 99ad9f74..b7dbbf8c 100644 --- a/td/telegram/files/FileDownloader.cpp +++ b/td/telegram/files/FileDownloader.cpp @@ -320,7 +320,7 @@ Result FileDownloader::process_part(Part part, NetQueryPtr net_query) { offset = ((offset & 0xff) << 24) | ((offset & 0xff00) << 8) | ((offset & 0xff0000) >> 8) | ((offset & 0xff000000) >> 24); as(iv.raw + 12) = offset; - auto key = as(cdn_encryption_key_.c_str()); + UInt256 key = as(cdn_encryption_key_.c_str()); AesCtrState ctr_state; ctr_state.init(key, iv); diff --git a/tdutils/td/utils/as.h b/tdutils/td/utils/as.h index cf8f17cb..9d2094d9 100644 --- a/tdutils/td/utils/as.h +++ b/tdutils/td/utils/as.h @@ -10,24 +10,31 @@ namespace td { +namespace detail { + template class As { public: - As(void *ptr) : ptr_(ptr) { + explicit As(void *ptr) : ptr_(ptr) { } + + As(const As &new_value) = delete; + As &operator=(const As &) = delete; As(As &&) = default; - As &operator=(const As &new_value) && { - memcpy(ptr_, new_value.ptr_, sizeof(T)); + As &operator=(As &&new_value) && { + std::memcpy(ptr_, new_value.ptr_, sizeof(T)); return *this; } - As &operator=(const T new_value) && { - memcpy(ptr_, &new_value, sizeof(T)); + ~As() = default; + + As &operator=(const T &new_value) && { + std::memcpy(ptr_, &new_value, sizeof(T)); return *this; } operator T() const { T res; - memcpy(&res, ptr_, sizeof(T)); + std::memcpy(&res, ptr_, sizeof(T)); return res; } @@ -38,12 +45,12 @@ class As { template class ConstAs { public: - ConstAs(const void *ptr) : ptr_(ptr) { + explicit ConstAs(const void *ptr) : ptr_(ptr) { } operator T() const { T res; - memcpy(&res, ptr_, sizeof(T)); + std::memcpy(&res, ptr_, sizeof(T)); return res; } @@ -51,14 +58,16 @@ class ConstAs { const void *ptr_; }; +} // namespace detail + template -As as(FromT *from) { - return As(from); +detail::As as(FromT *from) { + return detail::As(from); } template -const ConstAs as(const FromT *from) { - return ConstAs(from); +const detail::ConstAs as(const FromT *from) { + return detail::ConstAs(from); } } // namespace td