From c37909bde3f686b6e5d475ca54a9565132697ce4 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 16 Jan 2020 20:28:42 +0900 Subject: [PATCH] Add helpers converting network-order (big-endian) --- src/UDPC.h | 10 +++++++ src/UDPConnection.cpp | 29 ++++++++++++++++++++ src/test/TestUDPC.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/src/UDPC.h b/src/UDPC.h index 235ddac..4393700 100644 --- a/src/UDPC.h +++ b/src/UDPC.h @@ -649,11 +649,21 @@ const char *UDPC_atostr_cid(UDPC_HContext ctx, UDPC_ConnectionId connectionId); const char *UDPC_atostr(UDPC_HContext ctx, UDPC_IPV6_ADDR_TYPE addr); +// ============================================================================= +// Helpers + /// addrStr must be a valid ipv6 address or a valid ipv4 address UDPC_IPV6_ADDR_TYPE UDPC_strtoa(const char *addrStr); UDPC_IPV6_ADDR_TYPE UDPC_strtoa_link(const char *addrStr, uint32_t *linkId_out); +int UDPC_is_big_endian(); +uint16_t UDPC_no16i(uint16_t i); +uint32_t UDPC_no32i(uint32_t i); +uint64_t UDPC_no64i(uint64_t i); +float UDPC_no32f(float f); +double UDPC_no64f(double f); + #ifdef __cplusplus } #endif diff --git a/src/UDPConnection.cpp b/src/UDPConnection.cpp index 1a853a6..6d7ce8b 100644 --- a/src/UDPConnection.cpp +++ b/src/UDPConnection.cpp @@ -2932,3 +2932,32 @@ UDPC_IPV6_ADDR_TYPE UDPC_strtoa_link(const char *addrStr, uint32_t *linkId_out) checkSetOut(scope_id); return result; } + +int UDPC_is_big_endian() { + return UDPC::isBigEndian() ? 1 : 0; +} + +uint16_t UDPC_no16i(uint16_t i) { + return htons(i); +} + +uint32_t UDPC_no32i(uint32_t i) { + return htonl(i); +} + +uint64_t UDPC_no64i(uint64_t i) { + UDPC::be64((char*)&i); + return i; +} + +float UDPC_no32f(float f) { + uint32_t *i = reinterpret_cast(&f); + *i = htonl(*i); + return f; +} + +double UDPC_no64f(double f) { + uint64_t *i = reinterpret_cast(&f); + UDPC::be64((char*)i); + return f; +} diff --git a/src/test/TestUDPC.cpp b/src/test/TestUDPC.cpp index 5e4c98c..6c7ecd6 100644 --- a/src/test/TestUDPC.cpp +++ b/src/test/TestUDPC.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -257,3 +258,63 @@ TEST(UDPC, ConnectionIdBits) { EXPECT_EQ(((char*)&id)[i], 0); } } + +TEST(UDPC, NetworkOrderEndianness) { + if(UDPC_is_big_endian() != 0) { + puts("Is big-endian"); + uint16_t s = 0x0102; + s = UDPC_no16i(s); + EXPECT_EQ(s, 0x0102); + + uint32_t l = 0x01020304; + l = UDPC_no32i(l); + EXPECT_EQ(l, 0x01020304); + + uint64_t ll = 0x0102030405060708; + ll = UDPC_no64i(ll); + EXPECT_EQ(ll, 0x0102030405060708); + + l = 0x40208040; + float *f = reinterpret_cast(&l); + *f = UDPC_no32f(*f); + EXPECT_EQ(l, 0x40208040); + + ll = 0x4000001010008040; + double *d = reinterpret_cast(&ll); + *d = UDPC_no64f(*d); + EXPECT_EQ(ll, 0x4000001010008040); + } else { + puts("Is NOT big-endian"); + uint16_t s = 0x0102; + s = UDPC_no16i(s); + EXPECT_EQ(s, 0x0201); + s = UDPC_no16i(s); + EXPECT_EQ(s, 0x0102); + + uint32_t l = 0x01020304; + l = UDPC_no32i(l); + EXPECT_EQ(l, 0x04030201); + l = UDPC_no32i(l); + EXPECT_EQ(l, 0x01020304); + + uint64_t ll = 0x0102030405060708; + ll = UDPC_no64i(ll); + EXPECT_EQ(ll, 0x0807060504030201); + ll = UDPC_no64i(ll); + EXPECT_EQ(ll, 0x0102030405060708); + + l = 0x40208040; + float *f = reinterpret_cast(&l); + *f = UDPC_no32f(*f); + EXPECT_EQ(l, 0x40802040); + *f = UDPC_no32f(*f); + EXPECT_EQ(l, 0x40208040); + + ll = 0x4000001010008040; + double *d = reinterpret_cast(&ll); + *d = UDPC_no64f(*d); + EXPECT_EQ(ll, 0x4080001010000040); + *d = UDPC_no64f(*d); + EXPECT_EQ(ll, 0x4000001010008040); + } +} -- 2.49.0