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) {
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 ||
first_int == 0xdddddddd || first_int == 0xeeeeeeee) {
continue;
}
auto second_int = as<uint32>(header.data() + sizeof(uint32));
uint32 second_int = as<uint32>(header.data() + sizeof(uint32));
if (second_int == 0) {
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() << "]");
}
auto code = as<int32>(message.begin());
int32 code = as<int32>(message.begin());
if (code == 0) {
return ReadResult::make_nop();
} else if (code == -1 && message.size() >= 8) {

View File

@ -865,7 +865,7 @@ Result<std::tuple<uint64, BufferSlice, int32>> SecretChatActor::decrypt(BufferSl
UNREACHABLE();
}
auto len = as<int32>(data.begin());
int32 len = as<int32>(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);

View File

@ -184,7 +184,7 @@ Result<Secret> Secret::create(Slice secret) {
UInt256 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};
}

View File

@ -320,7 +320,7 @@ Result<size_t> FileDownloader::process_part(Part part, NetQueryPtr net_query) {
offset =
((offset & 0xff) << 24) | ((offset & 0xff00) << 8) | ((offset & 0xff0000) >> 8) | ((offset & 0xff000000) >> 24);
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;
ctr_state.init(key, iv);

View File

@ -10,24 +10,31 @@
namespace td {
namespace detail {
template <class T>
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<T> &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<T> &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 T>
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 <class ToT, class FromT>
As<ToT> as(FromT *from) {
return As<ToT>(from);
detail::As<ToT> as(FromT *from) {
return detail::As<ToT>(from);
}
template <class ToT, class FromT>
const ConstAs<ToT> as(const FromT *from) {
return ConstAs<ToT>(from);
const detail::ConstAs<ToT> as(const FromT *from) {
return detail::ConstAs<ToT>(from);
}
} // namespace td