Improve as<>.

GitOrigin-RevId: 227b2bae3079bed93936db16c4846c8d0a49bd39
This commit is contained in:
levlam 2018-12-20 01:46:55 +03:00
parent 3850a4da7f
commit e2d1a71d3b
6 changed files with 27 additions and 18 deletions

View File

@ -149,12 +149,12 @@ void ObfuscatedTransport::init(ChainBufferReader *input, ChainBufferWriter *outp
if (as<uint8>(header.data()) == 0xef) { if (as<uint8>(header.data()) == 0xef) {
continue; continue;
} }
auto first_int = as<uint32>(header.data()); uint32 first_int = as<uint32>(header.data());
if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f || if (first_int == 0x44414548 || first_int == 0x54534f50 || first_int == 0x20544547 || first_int == 0x4954504f ||
first_int == 0xdddddddd || first_int == 0xeeeeeeee) { first_int == 0xdddddddd || first_int == 0xeeeeeeee) {
continue; continue;
} }
auto second_int = as<uint32>(header.data() + sizeof(uint32)); uint32 second_int = as<uint32>(header.data() + sizeof(uint32));
if (second_int == 0) { if (second_int == 0) {
continue; continue;
} }

View File

@ -296,7 +296,7 @@ Result<Transport::ReadResult> Transport::read(MutableSlice message, const AuthKe
return Status::Error(PSLICE() << "Invalid mtproto message: smaller than 4 bytes [size=" << message.size() << "]"); return Status::Error(PSLICE() << "Invalid mtproto message: smaller than 4 bytes [size=" << message.size() << "]");
} }
auto code = as<int32>(message.begin()); int32 code = as<int32>(message.begin());
if (code == 0) { if (code == 0) {
return ReadResult::make_nop(); return ReadResult::make_nop();
} else if (code == -1 && message.size() >= 8) { } else if (code == -1 && message.size() >= 8) {

View File

@ -865,7 +865,7 @@ Result<std::tuple<uint64, BufferSlice, int32>> SecretChatActor::decrypt(BufferSl
UNREACHABLE(); UNREACHABLE();
} }
auto len = as<int32>(data.begin()); int32 len = as<int32>(data.begin());
data = data.substr(4, len); data = data.substr(4, len);
if (!is_aligned_pointer<4>(data.data())) { if (!is_aligned_pointer<4>(data.data())) {
return std::make_tuple(auth_key_id, BufferSlice(data), mtproto_version); return std::make_tuple(auth_key_id, BufferSlice(data), mtproto_version);

View File

@ -184,7 +184,7 @@ Result<Secret> Secret::create(Slice secret) {
UInt256 secret_sha256; UInt256 secret_sha256;
sha256(secret, ::td::as_slice(secret_sha256)); sha256(secret, ::td::as_slice(secret_sha256));
auto hash = as<int64>(secret_sha256.raw); int64 hash = as<int64>(secret_sha256.raw);
return Secret{res, hash}; return Secret{res, hash};
} }

View File

@ -320,7 +320,7 @@ Result<size_t> FileDownloader::process_part(Part part, NetQueryPtr net_query) {
offset = offset =
((offset & 0xff) << 24) | ((offset & 0xff00) << 8) | ((offset & 0xff0000) >> 8) | ((offset & 0xff000000) >> 24); ((offset & 0xff) << 24) | ((offset & 0xff00) << 8) | ((offset & 0xff0000) >> 8) | ((offset & 0xff000000) >> 24);
as<uint32>(iv.raw + 12) = offset; as<uint32>(iv.raw + 12) = offset;
auto key = as<UInt256>(cdn_encryption_key_.c_str()); UInt256 key = as<UInt256>(cdn_encryption_key_.c_str());
AesCtrState ctr_state; AesCtrState ctr_state;
ctr_state.init(key, iv); ctr_state.init(key, iv);

View File

@ -10,24 +10,31 @@
namespace td { namespace td {
namespace detail {
template <class T> template <class T>
class As { class As {
public: 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(As &&) = default;
As<T> &operator=(const As &new_value) && { As &operator=(As &&new_value) && {
memcpy(ptr_, new_value.ptr_, sizeof(T)); std::memcpy(ptr_, new_value.ptr_, sizeof(T));
return *this; return *this;
} }
As<T> &operator=(const T new_value) && { ~As() = default;
memcpy(ptr_, &new_value, sizeof(T));
As &operator=(const T &new_value) && {
std::memcpy(ptr_, &new_value, sizeof(T));
return *this; return *this;
} }
operator T() const { operator T() const {
T res; T res;
memcpy(&res, ptr_, sizeof(T)); std::memcpy(&res, ptr_, sizeof(T));
return res; return res;
} }
@ -38,12 +45,12 @@ class As {
template <class T> template <class T>
class ConstAs { class ConstAs {
public: public:
ConstAs(const void *ptr) : ptr_(ptr) { explicit ConstAs(const void *ptr) : ptr_(ptr) {
} }
operator T() const { operator T() const {
T res; T res;
memcpy(&res, ptr_, sizeof(T)); std::memcpy(&res, ptr_, sizeof(T));
return res; return res;
} }
@ -51,14 +58,16 @@ class ConstAs {
const void *ptr_; const void *ptr_;
}; };
} // namespace detail
template <class ToT, class FromT> template <class ToT, class FromT>
As<ToT> as(FromT *from) { detail::As<ToT> as(FromT *from) {
return As<ToT>(from); return detail::As<ToT>(from);
} }
template <class ToT, class FromT> template <class ToT, class FromT>
const ConstAs<ToT> as(const FromT *from) { const detail::ConstAs<ToT> as(const FromT *from) {
return ConstAs<ToT>(from); return detail::ConstAs<ToT>(from);
} }
} // namespace td } // namespace td