]> git.seodisparate.com - UDPConnection/commitdiff
Add helpers converting network-order (big-endian)
authorStephen Seo <seo.disparate@gmail.com>
Thu, 16 Jan 2020 11:28:42 +0000 (20:28 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 16 Jan 2020 11:28:42 +0000 (20:28 +0900)
src/UDPC.h
src/UDPConnection.cpp
src/test/TestUDPC.cpp

index 235ddac971f425238a746502dd58215db3d17c2e..4393700de91477f2a03f71256795be7d136c430c 100644 (file)
@@ -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
index 1a853a673029fd1fd81c4f197c45395c8eb198c0..6d7ce8b99cbdb7339a3a82aa30c036acbb1bac13 100644 (file)
@@ -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<uint32_t*>(&f);
+    *i = htonl(*i);
+    return f;
+}
+
+double UDPC_no64f(double f) {
+    uint64_t *i = reinterpret_cast<uint64_t*>(&f);
+    UDPC::be64((char*)i);
+    return f;
+}
index 5e4c98c9cf195555f1bd00d0cddfd0357313616d..6c7ecd6f0e21e113be2eaea91c5673c4f54f19f1 100644 (file)
@@ -3,6 +3,7 @@
 #include <UDPC.h>
 #include <UDPC_Defines.hpp>
 
+#include <cstdio>
 #include <cstring>
 #include <future>
 
@@ -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<float*>(&l);
+        *f = UDPC_no32f(*f);
+        EXPECT_EQ(l, 0x40208040);
+
+        ll = 0x4000001010008040;
+        double *d = reinterpret_cast<double*>(&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<float*>(&l);
+        *f = UDPC_no32f(*f);
+        EXPECT_EQ(l, 0x40802040);
+        *f = UDPC_no32f(*f);
+        EXPECT_EQ(l, 0x40208040);
+
+        ll = 0x4000001010008040;
+        double *d = reinterpret_cast<double*>(&ll);
+        *d = UDPC_no64f(*d);
+        EXPECT_EQ(ll, 0x4080001010000040);
+        *d = UDPC_no64f(*d);
+        EXPECT_EQ(ll, 0x4000001010008040);
+    }
+}