types: Migrate to stdplus endian
Change-Id: Idd87f83672b7f97a8bdd13f7646760c0d77c5ff5
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/types.hpp b/src/types.hpp
index f7c0fa3..7db6a37 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -3,6 +3,7 @@
#include <netinet/in.h>
#include <stdplus/net/addr/ether.hpp>
+#include <stdplus/numeric/endian.hpp>
#include <algorithm>
#include <array>
@@ -131,91 +132,6 @@
namespace detail
{
-
-template <typename T, uint8_t size = sizeof(T)>
-struct BswapAlign
-{
- using type = T;
-};
-
-template <typename T>
-struct BswapAlign<T, 2>
-{
- using type alignas(uint16_t) = T;
-};
-
-template <typename T>
-struct BswapAlign<T, 4>
-{
- using type alignas(uint32_t) = T;
-};
-
-template <typename T>
-struct BswapAlign<T, 8>
-{
- using type alignas(uint64_t) = T;
-};
-
-template <typename T>
-constexpr T bswapInt(typename BswapAlign<T>::type n) noexcept
-{
- static_assert(std::is_trivially_copyable_v<T>);
- if constexpr (sizeof(T) == 2)
- {
- reinterpret_cast<uint16_t&>(n) =
- __builtin_bswap16(reinterpret_cast<uint16_t&>(n));
- }
- else if constexpr (sizeof(T) == 4)
- {
- reinterpret_cast<uint32_t&>(n) =
- __builtin_bswap32(reinterpret_cast<uint32_t&>(n));
- }
- else if constexpr (sizeof(T) == 8)
- {
- reinterpret_cast<uint64_t&>(n) =
- __builtin_bswap64(reinterpret_cast<uint64_t&>(n));
- }
- else
- {
- auto b = reinterpret_cast<std::byte*>(&n);
- std::reverse(b, b + sizeof(n));
- }
- return n;
-}
-
-} // namespace detail
-
-template <typename T>
-constexpr T bswap(T n) noexcept
-{
- return detail::bswapInt<T>(n);
-}
-
-template <typename T>
-constexpr T hton(T n) noexcept
-{
- if constexpr (std::endian::native == std::endian::big)
- {
- return n;
- }
- else if constexpr (std::endian::native == std::endian::little)
- {
- return bswap(n);
- }
- else
- {
- static_assert(std::is_same_v<T, void>);
- }
-}
-
-template <typename T>
-constexpr T ntoh(T n) noexcept
-{
- return hton(n);
-}
-
-namespace detail
-{
inline constexpr auto charLookup = []() {
std::array<int8_t, 256> ret;
std::fill(ret.begin(), ret.end(), -1);
@@ -369,7 +285,7 @@
}
}
addr |= di(str);
- return {hton(addr)};
+ return {stdplus::hton(addr)};
}
};
@@ -391,7 +307,7 @@
}
if (loc != 0 && !str.empty())
{
- ret.s6_addr16[i++] = hton(di(str.substr(0, loc)));
+ ret.s6_addr16[i++] = stdplus::hton(di(str.substr(0, loc)));
}
if (i < 8 && str.size() > loc + 1 && str[loc + 1] == ':')
{
@@ -422,7 +338,7 @@
{
auto loc = str.rfind(':');
ret.s6_addr16[j--] =
- hton(di(str.substr(loc == str.npos ? 0 : loc + 1)));
+ stdplus::hton(di(str.substr(loc == str.npos ? 0 : loc + 1)));
str.remove_suffix(loc == str.npos ? str.size() : str.size() - loc);
}
if (!str.empty())
@@ -487,7 +403,7 @@
constexpr char* operator()(char* buf, in_addr v) const noexcept
{
- auto n = bswap(ntoh(v.s_addr));
+ auto n = stdplus::bswap(stdplus::ntoh(v.s_addr));
for (size_t i = 0; i < 3; ++i)
{
buf = ToStr<char>{}(EncodeInt<uint8_t, 10>{}(buf, n & 0xff), '.');
@@ -508,7 +424,7 @@
{
// IPv4 in IPv6 Addr
if (v.s6_addr32[0] == 0 && v.s6_addr32[1] == 0 &&
- v.s6_addr32[2] == hton(uint32_t(0xffff)))
+ v.s6_addr32[2] == stdplus::hton(uint32_t(0xffff)))
{
constexpr auto prefix = std::string_view("::ffff:");
return ToStr<in_addr>{}(
@@ -553,7 +469,7 @@
i += skip_size - 1;
continue;
}
- buf = EncodeInt<uint16_t, 16>{}(buf, ntoh(v.s6_addr16[i]));
+ buf = EncodeInt<uint16_t, 16>{}(buf, stdplus::ntoh(v.s6_addr16[i]));
if (i < 7)
{
*(buf++) = ':';
diff --git a/test/test_rtnetlink.cpp b/test/test_rtnetlink.cpp
index a0dddfe..d5de216 100644
--- a/test/test_rtnetlink.cpp
+++ b/test/test_rtnetlink.cpp
@@ -91,7 +91,7 @@
EXPECT_EQ(msg.ifa.ifa_flags, ret.flags);
EXPECT_EQ(msg.ifa.ifa_scope, ret.scope);
EXPECT_EQ(msg.ifa.ifa_index, ret.ifidx);
- EXPECT_EQ((IfAddr{in_addr{hton(0xc0a80114)}, 28}), ret.ifaddr);
+ EXPECT_EQ((IfAddr{in_addr{stdplus::hton(0xc0a80114)}, 28}), ret.ifaddr);
}
TEST(AddrFromRtm, ExtraFlags)
@@ -139,7 +139,7 @@
auto ret = neighFromRtm(stdplus::raw::asView<char>(msg));
EXPECT_EQ(msg.ndm.ndm_state, ret.state);
- EXPECT_EQ((in_addr{hton(0xc0a80114)}), ret.addr);
+ EXPECT_EQ((in_addr{stdplus::hton(0xc0a80114)}), ret.addr);
EXPECT_FALSE(ret.mac);
}
@@ -160,7 +160,7 @@
msg.mac_hdr.rta_len = RTA_LENGTH(sizeof(msg.mac));
auto ret = neighFromRtm(stdplus::raw::asView<char>(msg));
- EXPECT_EQ((in_addr{hton(0xc0a80114)}), ret.addr);
+ EXPECT_EQ((in_addr{stdplus::hton(0xc0a80114)}), ret.addr);
EXPECT_EQ((ether_addr{1, 2, 3, 4, 5, 6}), ret.mac);
}
diff --git a/test/test_types.cpp b/test/test_types.cpp
index 53d05a3..675ee19 100644
--- a/test/test_types.cpp
+++ b/test/test_types.cpp
@@ -30,19 +30,6 @@
namespace phosphor::network
{
-TEST(Byteswap, Swap)
-{
- EXPECT_EQ(38, bswap(uint8_t{38}));
- EXPECT_EQ(38 << 8, bswap(uint16_t{38}));
- EXPECT_EQ(0x240082fe, bswap(uint32_t{0xfe820024}));
- EXPECT_EQ(0x240082fe00000000, bswap(uint64_t{0xfe820024}));
- struct
- {
- std::array<char, 4> a = {1, 2, 3, 4};
- } s;
- EXPECT_EQ((std::array<char, 4>{4, 3, 2, 1}), bswap(s).a);
-}
-
TEST(DecodeInt, uint8_10)
{
DecodeInt<uint8_t, 10> d;