1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net.conv: use a pure v implementation instead of C.hton etc (#18226)

This commit is contained in:
kbkpbot 2023-05-22 11:59:33 +08:00 committed by GitHub
parent ce0591da8d
commit a10690b934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 84 deletions

14
vlib/net/conv/README.md Normal file
View File

@ -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.

View File

@ -1,9 +0,0 @@
module conv
#include <arpa/inet.h>
fn C.htonl(host u32) u32
fn C.htons(host u16) u16
fn C.ntohl(net u32) u32
fn C.ntohs(net u16) u16

View File

@ -1,15 +0,0 @@
module conv
#include <winsock2.h>
#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

View File

@ -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 }
}

66
vlib/net/conv/conv.v Normal file
View File

@ -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)
}