diff --git a/vlib/net/conv/README.md b/vlib/net/conv/README.md new file mode 100644 index 0000000000..6d31db85da --- /dev/null +++ b/vlib/net/conv/README.md @@ -0,0 +1,14 @@ +## Description: + +`net.conv` provides a convenient way to convert number values *to*, +and *from* the network byte order format (which is always big endian). + +When communicating across a network, it is possible that the machines +use different byte orders, since the host format of each system can +vary, depending on the CPU, and on most systems, is usually +little endian. + +To avoid mismatches due to that, the network byte order is used by +convention to send network data in a manner that will be received +coherently, regardless of the endianness of the sender system and +the receiver system. diff --git a/vlib/net/conv/c_default.c.v b/vlib/net/conv/c_default.c.v deleted file mode 100644 index 76c4be0ca9..0000000000 --- a/vlib/net/conv/c_default.c.v +++ /dev/null @@ -1,9 +0,0 @@ -module conv - -#include - -fn C.htonl(host u32) u32 -fn C.htons(host u16) u16 - -fn C.ntohl(net u32) u32 -fn C.ntohs(net u16) u16 diff --git a/vlib/net/conv/c_windows.c.v b/vlib/net/conv/c_windows.c.v deleted file mode 100644 index ae0f93e3a1..0000000000 --- a/vlib/net/conv/c_windows.c.v +++ /dev/null @@ -1,15 +0,0 @@ -module conv - -#include - -#flag -lws2_32 - -fn C.htonl(host u32) u32 -fn C.htons(host u16) u16 - -fn C.ntohl(net u32) u32 -fn C.ntohs(net u16) u16 - -//** These are not supplied with mingw/gcc ** -// fn C.htonll(host u64) u64 -// fn C.ntohll(net u64) u64 diff --git a/vlib/net/conv/conv.c.v b/vlib/net/conv/conv.c.v deleted file mode 100644 index ab66f74174..0000000000 --- a/vlib/net/conv/conv.c.v +++ /dev/null @@ -1,60 +0,0 @@ -module conv - -// host to net 32 (htonl) -pub fn htn32(host u32) u32 { - return C.htonl(host) -} - -// host to net 16 (htons) -pub fn htn16(host u16) u16 { - return C.htons(host) -} - -// net to host 32 (ntohl) -pub fn nth32(host u32) u32 { - return C.ntohl(host) -} - -// net to host 16 (ntohs) -pub fn nth16(host u16) u16 { - return C.ntohs(host) -} - -//****************************** - -struct Bytes { -mut: - first u32 - last u32 -} - -union LongLong { - Bytes - ll u64 -} - -// host to net 64 (htonll) -pub fn htn64(host u64) u64 { - mut ll := LongLong{ - ll: host - } - - unsafe { - ll.first = htn32(ll.first) - ll.last = htn32(ll.last) - } - return unsafe { ll.ll } -} - -// net to host 64 (ntohll) -pub fn nth64(net u64) u64 { - mut ll := LongLong{ - ll: net - } - - unsafe { - ll.first = nth32(ll.first) - ll.last = nth32(ll.last) - } - return unsafe { ll.ll } -} diff --git a/vlib/net/conv/conv.v b/vlib/net/conv/conv.v new file mode 100644 index 0000000000..d1c23d788f --- /dev/null +++ b/vlib/net/conv/conv.v @@ -0,0 +1,66 @@ +module conv + +// htn64 converts a the 64 bit value `host` to the net format (htonll) +pub fn htn64(host u64) u64 { + $if little_endian { + // vfmt off + return ( + ((host >> 56) & 0x00000000_000000FF) | + ((host >> 40) & 0x00000000_0000FF00) | + ((host >> 24) & 0x00000000_00FF0000) | + ((host >> 8) & 0x00000000_FF000000) | + ((host << 8) & 0x000000FF_00000000) | + ((host << 24) & 0x0000FF00_00000000) | + ((host << 40) & 0x00FF0000_00000000) | + ((host << 56) & 0xFF000000_00000000) + ) + // vfmt on + } $else { + return host + } +} + +// htn32 converts the 32 bit value `host` to the net format (htonl) +pub fn htn32(host u32) u32 { + $if little_endian { + // vfmt off + return ( + ((host >> 24) & 0x0000_00FF) | + ((host >> 8) & 0x0000_FF00) | + ((host << 8) & 0x00FF_0000) | + ((host << 24) & 0xFF00_0000) + ) + // vfmt on + } $else { + return host + } +} + +// htn16 converts the 16 bit value `host` to the net format (htons) +pub fn htn16(host u16) u16 { + $if little_endian { + // vfmt off + return ( + ((host >> 8) & 0x00FF) | + ((host << 8) & 0xFF00) + ) + // vfmt on + } $else { + return host + } +} + +// nth64 converts the 64 bit value `net` to the host format (ntohll) +pub fn nth64(net u64) u64 { + return htn64(net) +} + +// nth32 converts the 32 bit value `net` to the host format (ntohl) +pub fn nth32(net u32) u32 { + return htn32(net) +} + +// nth16 converts the 16 bit value `net` to the host format (ntohs) +pub fn nth16(net u16) u16 { + return htn16(net) +}