diff --git a/benchmark/bench_crypto.cpp b/benchmark/bench_crypto.cpp index 44d309ef..30acbc16 100644 --- a/benchmark/bench_crypto.cpp +++ b/benchmark/bench_crypto.cpp @@ -12,6 +12,7 @@ #include "td/utils/port/thread.h" #include "td/utils/Random.h" #include "td/utils/Slice.h" +#include "td/utils/UInt.h" #include diff --git a/td/mtproto/Handshake.h b/td/mtproto/Handshake.h index b167179a..6391943a 100644 --- a/td/mtproto/Handshake.h +++ b/td/mtproto/Handshake.h @@ -13,6 +13,7 @@ #include "td/utils/int_types.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" namespace td { diff --git a/td/mtproto/PingConnection.h b/td/mtproto/PingConnection.h index 68bbfb05..3e82cfe2 100644 --- a/td/mtproto/PingConnection.h +++ b/td/mtproto/PingConnection.h @@ -16,6 +16,7 @@ #include "td/utils/Random.h" #include "td/utils/Status.h" #include "td/utils/Time.h" +#include "td/utils/UInt.h" #include "td/mtproto/mtproto_api.h" diff --git a/td/mtproto/TcpTransport.h b/td/mtproto/TcpTransport.h index a63733f7..99e6db5e 100644 --- a/td/mtproto/TcpTransport.h +++ b/td/mtproto/TcpTransport.h @@ -15,6 +15,7 @@ #include "td/utils/crypto.h" #include "td/utils/port/detail/PollableFd.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" namespace td { namespace mtproto { diff --git a/td/mtproto/Transport.h b/td/mtproto/Transport.h index 543f5507..8ae5e475 100644 --- a/td/mtproto/Transport.h +++ b/td/mtproto/Transport.h @@ -13,6 +13,7 @@ #include "td/utils/logging.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" #include diff --git a/td/mtproto/crypto.h b/td/mtproto/crypto.h index 2358dd6c..2baac4bc 100644 --- a/td/mtproto/crypto.h +++ b/td/mtproto/crypto.h @@ -10,6 +10,7 @@ #include "td/utils/common.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" #include diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 423ab3d6..40b326cd 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -42,6 +42,7 @@ #include "td/utils/Time.h" #include "td/utils/tl_helpers.h" #include "td/utils/tl_parsers.h" +#include "td/utils/UInt.h" #include #include diff --git a/td/telegram/SecureStorage.h b/td/telegram/SecureStorage.h index 7430617b..815b667b 100644 --- a/td/telegram/SecureStorage.h +++ b/td/telegram/SecureStorage.h @@ -12,6 +12,7 @@ #include "td/utils/port/FileFd.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" namespace td { // Types diff --git a/td/telegram/files/FileDownloader.cpp b/td/telegram/files/FileDownloader.cpp index 1ea8a905..ff64de7e 100644 --- a/td/telegram/files/FileDownloader.cpp +++ b/td/telegram/files/FileDownloader.cpp @@ -23,6 +23,7 @@ #include "td/utils/port/Stat.h" #include "td/utils/ScopeGuard.h" #include "td/utils/Slice.h" +#include "td/utils/UInt.h" #include diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index ad940603..c4d56f59 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -24,6 +24,7 @@ #include "td/utils/StringBuilder.h" #include "td/utils/tl_helpers.h" #include "td/utils/tl_storers.h" +#include "td/utils/UInt.h" #include "td/utils/Variant.h" #include diff --git a/td/telegram/files/FileUploader.h b/td/telegram/files/FileUploader.h index 52328964..567d2c88 100644 --- a/td/telegram/files/FileUploader.h +++ b/td/telegram/files/FileUploader.h @@ -11,6 +11,7 @@ #include "td/utils/port/FileFd.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" #include diff --git a/td/tl/tl_object_parse.h b/td/tl/tl_object_parse.h index 0bcb7d34..73a8ca40 100644 --- a/td/tl/tl_object_parse.h +++ b/td/tl/tl_object_parse.h @@ -8,7 +8,7 @@ #include "td/tl/TlObject.h" -#include "td/utils/int_types.h" +#include "td/utils/UInt.h" #include #include diff --git a/tddb/td/db/binlog/Binlog.h b/tddb/td/db/binlog/Binlog.h index 89a44036..2d011e21 100644 --- a/tddb/td/db/binlog/Binlog.h +++ b/tddb/td/db/binlog/Binlog.h @@ -18,6 +18,7 @@ #include "td/utils/port/FileFd.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" #include diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index 1279e735..d477647d 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -207,6 +207,7 @@ set(TDUTILS_SOURCE td/utils/tl_storers.h td/utils/translit.h td/utils/type_traits.h + td/utils/UInt.h td/utils/unicode.h td/utils/unique_ptr.h td/utils/utf8.h diff --git a/tdutils/td/utils/AesCtrByteFlow.h b/tdutils/td/utils/AesCtrByteFlow.h index 820aa02f..bd4cee98 100644 --- a/tdutils/td/utils/AesCtrByteFlow.h +++ b/tdutils/td/utils/AesCtrByteFlow.h @@ -11,6 +11,7 @@ #include "td/utils/crypto.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" namespace td { diff --git a/tdutils/td/utils/Slice.h b/tdutils/td/utils/Slice.h index f80440b8..ae8da1b7 100644 --- a/tdutils/td/utils/Slice.h +++ b/tdutils/td/utils/Slice.h @@ -308,21 +308,4 @@ inline MutableSlice as_slice(MutableSlice slice) { return slice; } -template -Slice UInt::as_slice() const { - return Slice(raw, N / 8); -} -template -MutableSlice UInt::as_slice() { - return MutableSlice(raw, N / 8); -} -template -Slice as_slice(const UInt &value) { - return value.as_slice(); -} -template -MutableSlice as_slice(UInt &value) { - return value.as_slice(); -} - } // namespace td diff --git a/tdutils/td/utils/UInt.h b/tdutils/td/utils/UInt.h new file mode 100644 index 00000000..6bc7fecf --- /dev/null +++ b/tdutils/td/utils/UInt.h @@ -0,0 +1,93 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018 +// +// 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/common.h" +#include "td/utils/Slice.h" + +#include + +namespace td { + +template +struct UInt { + static_assert(size % 8 == 0, "size should be divisible by 8"); + uint8 raw[size / 8]; + + Slice as_slice() const { + return Slice(raw, size / 8); + } + + MutableSlice as_slice() { + return MutableSlice(raw, size / 8); + } + + bool is_zero() const { + for (size_t i = 0; i < size / 8; i++) { + if (raw[i] != 0) { + return false; + } + } + return true; + } + void set_zero() { + for (size_t i = 0; i < size / 8; i++) { + raw[i] = 0; + } + } + static UInt zero() { + UInt v; + v.set_zero(); + return v; + } +}; + +template +bool operator==(const UInt &a, const UInt &b) { + return std::memcmp(a.raw, b.raw, sizeof(a.raw)) == 0; +} + +template +bool operator!=(const UInt &a, const UInt &b) { + return !(a == b); +} + +template +td::UInt operator^(const UInt &a, const UInt &b) { + td::UInt res; + for (size_t i = 0; i < size / 8; i++) { + res.raw[i] = static_cast(a.raw[i] ^ b.raw[i]); + } + return res; +} + +template +int get_kth_bit(const UInt &a, uint32 bit) { + uint8 b = a.raw[bit / 8]; + bit &= 7; + return (b >> (7 - bit)) & 1; +} + +template +Slice as_slice(const UInt &value) { + return value.as_slice(); +} + +template +MutableSlice as_slice(UInt &value) { + return value.as_slice(); +} + +template +bool operator<(const UInt &a, const UInt &b) { + return std::memcmp(a.raw, b.raw, sizeof(a.raw)) < 0; +} + +using UInt128 = UInt<128>; +using UInt256 = UInt<256>; + +} // namespace td diff --git a/tdutils/td/utils/common.h b/tdutils/td/utils/common.h index 7ee8e621..a9f7630c 100644 --- a/tdutils/td/utils/common.h +++ b/tdutils/td/utils/common.h @@ -45,6 +45,7 @@ #include "td/utils/int_types.h" #include "td/utils/unique_ptr.h" +#include // temporary for std::memcpy #include #include diff --git a/tdutils/td/utils/crypto.h b/tdutils/td/utils/crypto.h index e7259692..fb8d4620 100644 --- a/tdutils/td/utils/crypto.h +++ b/tdutils/td/utils/crypto.h @@ -10,6 +10,7 @@ #include "td/utils/common.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" namespace td { diff --git a/tdutils/td/utils/int_types.h b/tdutils/td/utils/int_types.h index 08dda747..cc1b3dce 100644 --- a/tdutils/td/utils/int_types.h +++ b/tdutils/td/utils/int_types.h @@ -10,7 +10,6 @@ #include #include -#include namespace td { @@ -43,67 +42,4 @@ static_assert(static_cast(-256) == 0, "Unexpected cast to char implementat #pragma warning(pop) #endif -class Slice; -class MutableSlice; -template -struct UInt { - static_assert(size % 8 == 0, "size should be divisible by 8"); - uint8 raw[size / 8]; - Slice as_slice() const; - MutableSlice as_slice(); - - bool is_zero() const { - for (size_t i = 0; i * 8 < size; i++) { - if (raw[i] != 0) { - return false; - } - } - return true; - } - void set_zero() { - for (size_t i = 0; i * 8 < size; i++) { - raw[i] = 0; - } - } - static UInt zero() { - UInt v; - v.set_zero(); - return v; - } -}; - -template -bool operator==(const UInt &a, const UInt &b) { - return std::memcmp(a.raw, b.raw, sizeof(a.raw)) == 0; -} - -template -bool operator!=(const UInt &a, const UInt &b) { - return !(a == b); -} - -template -td::UInt operator^(const UInt &a, const UInt &b) { - td::UInt res; - for (size_t i = 0; i * 8 < size; i++) { - res.raw[i] = static_cast(a.raw[i] ^ b.raw[i]); - } - return res; -} - -template -int get_kth_bit(const UInt &a, uint32 bit) { - uint8 b = a.raw[bit / 8]; - bit &= 7; - return (b >> (7 - bit)) & 1; -} - -template -bool operator<(const UInt &a, const UInt &b) { - return std::memcmp(a.raw, b.raw, sizeof(a.raw)) < 0; -} - -using UInt128 = UInt<128>; -using UInt256 = UInt<256>; - } // namespace td diff --git a/tdutils/td/utils/tl_parsers.h b/tdutils/td/utils/tl_parsers.h index 0c882265..3df28255 100644 --- a/tdutils/td/utils/tl_parsers.h +++ b/tdutils/td/utils/tl_parsers.h @@ -13,6 +13,7 @@ #include "td/utils/misc.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" #include "td/utils/utf8.h" #include diff --git a/tdutils/td/utils/tl_storers.h b/tdutils/td/utils/tl_storers.h index 1411b40c..c00fcd1e 100644 --- a/tdutils/td/utils/tl_storers.h +++ b/tdutils/td/utils/tl_storers.h @@ -12,6 +12,7 @@ #include "td/utils/misc.h" #include "td/utils/Slice.h" #include "td/utils/StorerBase.h" +#include "td/utils/UInt.h" #include diff --git a/tdutils/test/crypto.cpp b/tdutils/test/crypto.cpp index c3400465..03529954 100644 --- a/tdutils/test/crypto.cpp +++ b/tdutils/test/crypto.cpp @@ -9,6 +9,7 @@ #include "td/utils/crypto.h" #include "td/utils/Slice.h" #include "td/utils/tests.h" +#include "td/utils/UInt.h" #include diff --git a/test/http.cpp b/test/http.cpp index 90d37b58..099f2c40 100644 --- a/test/http.cpp +++ b/test/http.cpp @@ -30,6 +30,7 @@ #include "td/utils/Random.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" +#include "td/utils/UInt.h" #include "test/data.h"