v
Зеркало из https://github.com/vlang/v
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module binary
5
6// big_endian_u16 creates a u16 from the first two bytes in the array b in big endian order.
7@[direct_array_access; inline]
8pub fn big_endian_u16(b []u8) u16 {
9_ = b[1] // bounds check
10return u16(b[1]) | (u16(b[0]) << u16(8))
11}
12
13// big_endian_u16_at creates a u16 from two bytes in the array b at the specified offset in big endian order.
14@[direct_array_access; inline]
15pub fn big_endian_u16_at(b []u8, o int) u16 {
16_ = b[o] // bounds check
17_ = b[o + 1] // bounds check
18return u16(b[o + 1]) | (u16(b[o]) << u16(8))
19}
20
21// big_endian_u16_end creates a u16 from two bytes in the array b at the specified offset in big endian order.
22@[direct_array_access; inline]
23pub fn big_endian_u16_end(b []u8) u16 {
24return big_endian_u16_at(b, b.len - 2)
25}
26
27// big_endian_put_u16 writes a u16 to the first two bytes in the array b in big endian order.
28@[direct_array_access; inline]
29pub fn big_endian_put_u16(mut b []u8, v u16) {
30_ = b[1] // bounds check
31b[0] = u8(v >> u16(8))
32b[1] = u8(v)
33}
34
35// big_endian_put_u16_at writes a u16 to the two bytes in the array b at the specified offset in big endian order.
36@[direct_array_access; inline]
37pub fn big_endian_put_u16_at(mut b []u8, v u16, o int) {
38_ = b[o] // bounds check
39_ = b[o + 1] // bounds check
40b[o] = u8(v >> u16(8))
41b[o + 1] = u8(v)
42}
43
44// big_endian_put_u16_end writes a u16 to the last two bytes in the array b in big endian order.
45@[direct_array_access; inline]
46pub fn big_endian_put_u16_end(mut b []u8, v u16) {
47big_endian_put_u16_at(mut b, v, b.len - 2)
48}
49
50// big_endian_get_u16 creates u8 array from the unsigned 16-bit integer v in big endian order.
51pub fn big_endian_get_u16(v u16) []u8 {
52mut b := []u8{cap: 2}
53b << u8(v >> u16(8))
54b << u8(v)
55return b
56}
57
58// big_endian_u32 creates a u32 from four bytes in the array b in big endian order.
59@[direct_array_access; inline]
60pub fn big_endian_u32(b []u8) u32 {
61_ = b[3] // bounds check
62return u32(b[3]) | (u32(b[2]) << u32(8)) | (u32(b[1]) << u32(16)) | (u32(b[0]) << u32(24))
63}
64
65// big_endian_u32_at creates a u32 from four bytes in the array b at the specified offset in big endian order.
66@[direct_array_access; inline]
67pub fn big_endian_u32_at(b []u8, o int) u32 {
68_ = b[o] // bounds check
69_ = b[o + 3] // bounds check
70return u32(b[o + 3]) | (u32(b[o + 2]) << u32(8)) | (u32(b[o + 1]) << u32(16)) | (u32(b[o]) << u32(24))
71}
72
73// big_endian_u32_end creates a u32 from the last four bytes in the array b in big endian order.
74@[direct_array_access; inline]
75pub fn big_endian_u32_end(b []u8) u32 {
76return big_endian_u32_at(b, b.len - 4)
77}
78
79// big_endian_put_u32 writes a u32 to the first four bytes in the array b in big endian order.
80@[direct_array_access; inline]
81pub fn big_endian_put_u32(mut b []u8, v u32) {
82_ = b[3] // bounds check
83b[0] = u8(v >> u32(24))
84b[1] = u8(v >> u32(16))
85b[2] = u8(v >> u32(8))
86b[3] = u8(v)
87}
88
89// big_endian_put_u32_at writes a u32 to four bytes in the array b at the specified offset in big endian order.
90@[direct_array_access; inline]
91pub fn big_endian_put_u32_at(mut b []u8, v u32, o int) {
92_ = b[o] // bounds check
93_ = b[o + 3] // bounds check
94b[o] = u8(v >> u32(24))
95b[o + 1] = u8(v >> u32(16))
96b[o + 2] = u8(v >> u32(8))
97b[o + 3] = u8(v)
98}
99
100// big_endian_put_u32_end writes a u32 to the last four bytes in the array b in big endian order.
101@[direct_array_access; inline]
102pub fn big_endian_put_u32_end(mut b []u8, v u32) {
103big_endian_put_u32_at(mut b, v, b.len - 4)
104}
105
106// big_endian_get_u32 creates u8 array from the unsigned 32-bit integer v in big endian order.
107pub fn big_endian_get_u32(v u32) []u8 {
108mut b := []u8{cap: 4}
109b << u8(v >> u32(24))
110b << u8(v >> u32(16))
111b << u8(v >> u32(8))
112b << u8(v)
113return b
114}
115
116// big_endian_u64 creates a u64 from the first eight bytes in the array b in big endian order.
117@[direct_array_access; inline]
118pub fn big_endian_u64(b []u8) u64 {
119_ = b[7] // bounds check
120return u64(b[7]) | (u64(b[6]) << u64(8)) | (u64(b[5]) << u64(16)) | (u64(b[4]) << u64(24)) | (u64(b[3]) << u64(32)) | (u64(b[2]) << u64(40)) | (u64(b[1]) << u64(48)) | (u64(b[0]) << u64(56))
121}
122
123// big_endian_u64_at creates a u64 from eight bytes in the array b at the specified offset in big endian order.
124@[direct_array_access; inline]
125pub fn big_endian_u64_at(b []u8, o int) u64 {
126_ = b[o] // bounds check
127_ = b[o + 7] // bounds check
128return u64(b[o + 7]) | (u64(b[o + 6]) << u64(8)) | (u64(b[o + 5]) << u64(16)) | (u64(b[o + 4]) << u64(24)) | (u64(b[
129o + 3]) << u64(32)) | (u64(b[o + 2]) << u64(40)) | (u64(b[o + 1]) << u64(48)) | (u64(b[o]) << u64(56))
130}
131
132// big_endian_u64_end creates a u64 from the last eight bytes in the array b in big endian order.
133@[direct_array_access; inline]
134pub fn big_endian_u64_end(b []u8) u64 {
135return big_endian_u64_at(b, b.len - 8)
136}
137
138// big_endian_put_u64 writes a u64 to the first eight bytes in the array b in big endian order.
139@[direct_array_access; inline]
140pub fn big_endian_put_u64(mut b []u8, v u64) {
141_ = b[7] // bounds check
142b[0] = u8(v >> u64(56))
143b[1] = u8(v >> u64(48))
144b[2] = u8(v >> u64(40))
145b[3] = u8(v >> u64(32))
146b[4] = u8(v >> u64(24))
147b[5] = u8(v >> u64(16))
148b[6] = u8(v >> u64(8))
149b[7] = u8(v)
150}
151
152// big_endian_put_u64_at writes a u64 to eight bytes in the array b at the specified offset in big endian order.
153@[direct_array_access; inline]
154pub fn big_endian_put_u64_at(mut b []u8, v u64, o int) {
155_ = b[o] // bounds check
156_ = b[o + 7] // bounds check
157b[o] = u8(v >> u64(56))
158b[o + 1] = u8(v >> u64(48))
159b[o + 2] = u8(v >> u64(40))
160b[o + 3] = u8(v >> u64(32))
161b[o + 4] = u8(v >> u64(24))
162b[o + 5] = u8(v >> u64(16))
163b[o + 6] = u8(v >> u64(8))
164b[o + 7] = u8(v)
165}
166
167// big_endian_put_u64_end writes a u64 to the last eight bytes in the array b at the specified offset in big endian order.
168@[direct_array_access; inline]
169pub fn big_endian_put_u64_end(mut b []u8, v u64) {
170big_endian_put_u64_at(mut b, v, b.len - 8)
171}
172
173// big_endian_get_u64 creates u8 array from the unsigned 64-bit integer v in big endian order.
174pub fn big_endian_get_u64(v u64) []u8 {
175mut b := []u8{cap: 8}
176b << u8(v >> u64(56))
177b << u8(v >> u64(48))
178b << u8(v >> u64(40))
179b << u8(v >> u64(32))
180b << u8(v >> u64(24))
181b << u8(v >> u64(16))
182b << u8(v >> u64(8))
183b << u8(v)
184return b
185}
186