diff --git a/src/network/htonl.c b/src/network/htonl.c index b21dace04600aea25b95bcc9435a28f9ef6bfec2..6622d16cc5351749d1b76c5f24aa71009b018ff0 100644 --- a/src/network/htonl.c +++ b/src/network/htonl.c @@ -1,10 +1,8 @@ #include +#include uint32_t htonl(uint32_t n) { - union { - uint8_t b[4]; - uint32_t i; - } u = { { n>>24, n>>16, n>>8, n } }; - return u.i; + union { int i; char c; } u = { 1 }; + return u.c ? bswap_32(n) : n; } diff --git a/src/network/htons.c b/src/network/htons.c index 522504a5ddf25d0b95564a0122b61a926c1fe651..03a3a1d59ed4d5d10272d2751920ce3f8bec080b 100644 --- a/src/network/htons.c +++ b/src/network/htons.c @@ -1,10 +1,8 @@ #include +#include uint16_t htons(uint16_t n) { - union { - uint8_t b[2]; - uint16_t s; - } u = { { n>>8, n } }; - return u.s; + union { int i; char c; } u = { 1 }; + return u.c ? bswap_16(n) : n; } diff --git a/src/network/ntohl.c b/src/network/ntohl.c index 6437919667d84d28b95689962643e6d100997253..d6fce4590d43a3ee868d7cd807c89c0b6599ee3e 100644 --- a/src/network/ntohl.c +++ b/src/network/ntohl.c @@ -1,10 +1,8 @@ #include +#include uint32_t ntohl(uint32_t n) { - union { - uint32_t i; - uint8_t b[4]; - } u = { n }; - return (u.b[0]<<24) | (u.b[1]<<16) | (u.b[2]<<8) | u.b[3]; + union { int i; char c; } u = { 1 }; + return u.c ? bswap_32(n) : n; } diff --git a/src/network/ntohs.c b/src/network/ntohs.c index 3544a4798cfc43ea3f6fe8cddd698c78d66a0279..745cef425a1947f119880d499e7829146e049470 100644 --- a/src/network/ntohs.c +++ b/src/network/ntohs.c @@ -1,10 +1,8 @@ #include +#include uint16_t ntohs(uint16_t n) { - union { - uint16_t s; - uint8_t b[2]; - } u = { n }; - return (u.b[0]<<8) | u.b[1]; + union { int i; char c; } u = { 1 }; + return u.c ? bswap_16(n) : n; }