v

Зеркало из https://github.com/vlang/v
Форк
0
/
big_endian.v 
185 строк · 6.2 Кб
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.
4
module 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]
8
pub fn big_endian_u16(b []u8) u16 {
9
	_ = b[1] // bounds check
10
	return 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]
15
pub fn big_endian_u16_at(b []u8, o int) u16 {
16
	_ = b[o] // bounds check
17
	_ = b[o + 1] // bounds check
18
	return 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]
23
pub fn big_endian_u16_end(b []u8) u16 {
24
	return 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]
29
pub fn big_endian_put_u16(mut b []u8, v u16) {
30
	_ = b[1] // bounds check
31
	b[0] = u8(v >> u16(8))
32
	b[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]
37
pub fn big_endian_put_u16_at(mut b []u8, v u16, o int) {
38
	_ = b[o] // bounds check
39
	_ = b[o + 1] // bounds check
40
	b[o] = u8(v >> u16(8))
41
	b[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]
46
pub fn big_endian_put_u16_end(mut b []u8, v u16) {
47
	big_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.
51
pub fn big_endian_get_u16(v u16) []u8 {
52
	mut b := []u8{cap: 2}
53
	b << u8(v >> u16(8))
54
	b << u8(v)
55
	return 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]
60
pub fn big_endian_u32(b []u8) u32 {
61
	_ = b[3] // bounds check
62
	return 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]
67
pub fn big_endian_u32_at(b []u8, o int) u32 {
68
	_ = b[o] // bounds check
69
	_ = b[o + 3] // bounds check
70
	return 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]
75
pub fn big_endian_u32_end(b []u8) u32 {
76
	return 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]
81
pub fn big_endian_put_u32(mut b []u8, v u32) {
82
	_ = b[3] // bounds check
83
	b[0] = u8(v >> u32(24))
84
	b[1] = u8(v >> u32(16))
85
	b[2] = u8(v >> u32(8))
86
	b[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]
91
pub fn big_endian_put_u32_at(mut b []u8, v u32, o int) {
92
	_ = b[o] // bounds check
93
	_ = b[o + 3] // bounds check
94
	b[o] = u8(v >> u32(24))
95
	b[o + 1] = u8(v >> u32(16))
96
	b[o + 2] = u8(v >> u32(8))
97
	b[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]
102
pub fn big_endian_put_u32_end(mut b []u8, v u32) {
103
	big_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.
107
pub fn big_endian_get_u32(v u32) []u8 {
108
	mut b := []u8{cap: 4}
109
	b << u8(v >> u32(24))
110
	b << u8(v >> u32(16))
111
	b << u8(v >> u32(8))
112
	b << u8(v)
113
	return 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]
118
pub fn big_endian_u64(b []u8) u64 {
119
	_ = b[7] // bounds check
120
	return 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]
125
pub fn big_endian_u64_at(b []u8, o int) u64 {
126
	_ = b[o] // bounds check
127
	_ = b[o + 7] // bounds check
128
	return u64(b[o + 7]) | (u64(b[o + 6]) << u64(8)) | (u64(b[o + 5]) << u64(16)) | (u64(b[o + 4]) << u64(24)) | (u64(b[
129
		o + 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]
134
pub fn big_endian_u64_end(b []u8) u64 {
135
	return 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]
140
pub fn big_endian_put_u64(mut b []u8, v u64) {
141
	_ = b[7] // bounds check
142
	b[0] = u8(v >> u64(56))
143
	b[1] = u8(v >> u64(48))
144
	b[2] = u8(v >> u64(40))
145
	b[3] = u8(v >> u64(32))
146
	b[4] = u8(v >> u64(24))
147
	b[5] = u8(v >> u64(16))
148
	b[6] = u8(v >> u64(8))
149
	b[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]
154
pub fn big_endian_put_u64_at(mut b []u8, v u64, o int) {
155
	_ = b[o] // bounds check
156
	_ = b[o + 7] // bounds check
157
	b[o] = u8(v >> u64(56))
158
	b[o + 1] = u8(v >> u64(48))
159
	b[o + 2] = u8(v >> u64(40))
160
	b[o + 3] = u8(v >> u64(32))
161
	b[o + 4] = u8(v >> u64(24))
162
	b[o + 5] = u8(v >> u64(16))
163
	b[o + 6] = u8(v >> u64(8))
164
	b[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]
169
pub fn big_endian_put_u64_end(mut b []u8, v u64) {
170
	big_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.
174
pub fn big_endian_get_u64(v u64) []u8 {
175
	mut b := []u8{cap: 8}
176
	b << u8(v >> u64(56))
177
	b << u8(v >> u64(48))
178
	b << u8(v >> u64(40))
179
	b << u8(v >> u64(32))
180
	b << u8(v >> u64(24))
181
	b << u8(v >> u64(16))
182
	b << u8(v >> u64(8))
183
	b << u8(v)
184
	return b
185
}
186

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.