v2 / vlib / net / conv / conv.v
167 lines · 150 sloc · 3.65 KB · 8e35f4d9848f7ad35d857a187dddbfd2eca5e19d
Raw
1module conv
2
3union ConversionUnion {
4mut:
5 as_int64 u64
6 as_int32 u32
7 as_double64 f64
8 as_double32 f32
9}
10
11// htonf32 converts the 32 bit double `host` to the net format
12pub fn htonf32(host f32) f32 {
13 $if little_endian {
14 mut convert := ConversionUnion{
15 as_double32: host
16 }
17 convert.as_int32 = unsafe { hton32(convert.as_int32) }
18 return unsafe { convert.as_double32 }
19 } $else {
20 return host
21 }
22}
23
24// htonf64 converts the 64 bit double `host` to the net format
25pub fn htonf64(host f64) f64 {
26 $if little_endian {
27 mut convert := ConversionUnion{
28 as_double64: host
29 }
30 convert.as_int64 = unsafe { hton64(convert.as_int64) }
31 return unsafe { convert.as_double64 }
32 } $else {
33 return host
34 }
35}
36
37// hton64 converts the 64 bit value `host` to the net format (htonll)
38pub fn hton64(host u64) u64 {
39 $if little_endian {
40 return reverse_bytes_u64(host)
41 } $else {
42 return host
43 }
44}
45
46// hton32 converts the 32 bit value `host` to the net format (htonl)
47pub fn hton32(host u32) u32 {
48 $if little_endian {
49 return reverse_bytes_u32(host)
50 } $else {
51 return host
52 }
53}
54
55// hton16 converts the 16 bit value `host` to the net format (htons)
56pub fn hton16(host u16) u16 {
57 $if little_endian {
58 return reverse_bytes_u16(host)
59 } $else {
60 return host
61 }
62}
63
64// ntoh64 converts the 64 bit value `net` to the host format (ntohll)
65pub fn ntoh64(net u64) u64 {
66 return hton64(net)
67}
68
69// ntoh32 converts the 32 bit value `net` to the host format (ntohl)
70pub fn ntoh32(net u32) u32 {
71 return hton32(net)
72}
73
74// ntoh16 converts the 16 bit value `net` to the host format (ntohs)
75pub fn ntoh16(net u16) u16 {
76 return hton16(net)
77}
78
79// u64tovarint converts the given 64 bit number `n`, where n < 2^62 to a byte array,
80// using the variable length unsigned integer encoding from:
81// https://datatracker.ietf.org/doc/html/rfc9000#section-16 .
82// The returned array length .len, will be in [1,2,4,8] .
83pub fn u64tovarint(n u64) ![]u8 {
84 if n > u64(1) << 62 {
85 return error('cannot encode more than 2^62-1')
86 }
87 msb := match true {
88 n < 64 {
89 u8(0b00)
90 }
91 n < 16384 {
92 u8(0b01)
93 }
94 n < 1073741824 {
95 u8(0b10)
96 }
97 else {
98 u8(0b11)
99 }
100 }
101
102 len := 1 << msb
103 mut result := []u8{len: len}
104 mut tn := n
105 for i in 0 .. len {
106 result[len - 1 - i] = u8(tn % 256)
107 tn /= 256
108 }
109 result[0] |= msb << 6
110
111 return result
112}
113
114// varinttou64 parses a variable length number from the start of the
115// given byte array `b`. If it succeeds, it returns the decoded number,
116// and the length of the parsed byte span.
117pub fn varinttou64(b []u8) !(u64, u8) {
118 if b.len == 0 {
119 return error('cannot parse vluint from empty byte array')
120 }
121 msb := b[0] >> 6
122 len := u8(1 << msb)
123 if len > b.len {
124 return error('expected ${len} bytes but got ${b.len} bytes')
125 }
126 mut n := u64(b[0] & 0b00111111)
127
128 for i in 1 .. len {
129 n = n * 256 + b[i]
130 }
131 return n, len
132}
133
134// reverse_bytes_u64 reverse a u64's byte order
135@[inline]
136pub fn reverse_bytes_u64(a u64) u64 {
137 // vfmt off
138 return ((a >> 56) & 0x00000000_000000FF) |
139 ((a >> 40) & 0x00000000_0000FF00) |
140 ((a >> 24) & 0x00000000_00FF0000) |
141 ((a >> 8) & 0x00000000_FF000000) |
142 ((a << 8) & 0x000000FF_00000000) |
143 ((a << 24) & 0x0000FF00_00000000) |
144 ((a << 40) & 0x00FF0000_00000000) |
145 ((a << 56) & 0xFF000000_00000000)
146 // vfmt on
147}
148
149// reverse_bytes_u32 reverse a u32's byte order
150@[inline]
151pub fn reverse_bytes_u32(a u32) u32 {
152 // vfmt off
153 return ((a >> 24) & 0x0000_00FF) |
154 ((a >> 8) & 0x0000_FF00) |
155 ((a << 8) & 0x00FF_0000) |
156 ((a << 24) & 0xFF00_0000)
157 // vfmt on
158}
159
160// reverse_bytes_u16 reverse a u16's byte order
161@[inline]
162pub fn reverse_bytes_u16(a u16) u16 {
163 // vfmt off
164 return ((a >> 8) & 0x00FF) |
165 ((a << 8) & 0xFF00)
166 // vfmt on
167}
168