1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
package binint
import (
"encoding/binary"
"git.lukeshu.com/btrfs-progs-ng/lib/binstruct/binutil"
)
const (
sizeof8 = 1
sizeof16 = 2
sizeof32 = 4
sizeof64 = 8
)
// unsigned
type U8 uint8
func (U8) BinaryStaticSize() int { return sizeof8 }
func (x U8) MarshalBinary() ([]byte, error) { return []byte{byte(x)}, nil }
func (x *U8) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof8); err != nil {
return 0, err
}
*x = U8(dat[0])
return sizeof8, nil
}
// unsigned little endian
type U16le uint16
func (U16le) BinaryStaticSize() int { return sizeof16 }
func (x U16le) MarshalBinary() ([]byte, error) {
var buf [sizeof16]byte
binary.LittleEndian.PutUint16(buf[:], uint16(x))
return buf[:], nil
}
func (x *U16le) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof16); err != nil {
return 0, err
}
*x = U16le(binary.LittleEndian.Uint16(dat))
return sizeof16, nil
}
type U32le uint32
func (U32le) BinaryStaticSize() int { return sizeof32 }
func (x U32le) MarshalBinary() ([]byte, error) {
var buf [sizeof32]byte
binary.LittleEndian.PutUint32(buf[:], uint32(x))
return buf[:], nil
}
func (x *U32le) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof32); err != nil {
return 0, err
}
*x = U32le(binary.LittleEndian.Uint32(dat))
return sizeof32, nil
}
type U64le uint64
func (U64le) BinaryStaticSize() int { return sizeof64 }
func (x U64le) MarshalBinary() ([]byte, error) {
var buf [sizeof64]byte
binary.LittleEndian.PutUint64(buf[:], uint64(x))
return buf[:], nil
}
func (x *U64le) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof64); err != nil {
return 0, err
}
*x = U64le(binary.LittleEndian.Uint64(dat))
return sizeof64, nil
}
// unsigned big endian
type U16be uint16
func (U16be) BinaryStaticSize() int { return sizeof16 }
func (x U16be) MarshalBinary() ([]byte, error) {
var buf [sizeof16]byte
binary.BigEndian.PutUint16(buf[:], uint16(x))
return buf[:], nil
}
func (x *U16be) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof16); err != nil {
return 0, err
}
*x = U16be(binary.BigEndian.Uint16(dat))
return sizeof16, nil
}
type U32be uint32
func (U32be) BinaryStaticSize() int { return sizeof32 }
func (x U32be) MarshalBinary() ([]byte, error) {
var buf [sizeof32]byte
binary.BigEndian.PutUint32(buf[:], uint32(x))
return buf[:], nil
}
func (x *U32be) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof32); err != nil {
return 0, err
}
*x = U32be(binary.BigEndian.Uint32(dat))
return sizeof32, nil
}
type U64be uint64
func (U64be) BinaryStaticSize() int { return sizeof64 }
func (x U64be) MarshalBinary() ([]byte, error) {
var buf [sizeof64]byte
binary.BigEndian.PutUint64(buf[:], uint64(x))
return buf[:], nil
}
func (x *U64be) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof64); err != nil {
return 0, err
}
*x = U64be(binary.BigEndian.Uint64(dat))
return sizeof64, nil
}
// signed
type I8 int8
func (I8) BinaryStaticSize() int { return sizeof8 }
func (x I8) MarshalBinary() ([]byte, error) { return []byte{byte(x)}, nil }
func (x *I8) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof8); err != nil {
return 0, err
}
*x = I8(dat[0])
return sizeof8, nil
}
// signed little endian
type I16le int16
func (I16le) BinaryStaticSize() int { return sizeof16 }
func (x I16le) MarshalBinary() ([]byte, error) {
var buf [sizeof16]byte
binary.LittleEndian.PutUint16(buf[:], uint16(x))
return buf[:], nil
}
func (x *I16le) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof16); err != nil {
return 0, err
}
*x = I16le(binary.LittleEndian.Uint16(dat))
return sizeof16, nil
}
type I32le int32
func (I32le) BinaryStaticSize() int { return sizeof32 }
func (x I32le) MarshalBinary() ([]byte, error) {
var buf [sizeof32]byte
binary.LittleEndian.PutUint32(buf[:], uint32(x))
return buf[:], nil
}
func (x *I32le) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof32); err != nil {
return 0, err
}
*x = I32le(binary.LittleEndian.Uint32(dat))
return sizeof32, nil
}
type I64le int64
func (I64le) BinaryStaticSize() int { return sizeof64 }
func (x I64le) MarshalBinary() ([]byte, error) {
var buf [sizeof64]byte
binary.LittleEndian.PutUint64(buf[:], uint64(x))
return buf[:], nil
}
func (x *I64le) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof64); err != nil {
return 0, err
}
*x = I64le(binary.LittleEndian.Uint64(dat))
return sizeof64, nil
}
// signed big endian
type I16be int16
func (I16be) BinaryStaticSize() int { return sizeof16 }
func (x I16be) MarshalBinary() ([]byte, error) {
var buf [sizeof16]byte
binary.BigEndian.PutUint16(buf[:], uint16(x))
return buf[:], nil
}
func (x *I16be) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof16); err != nil {
return 0, err
}
*x = I16be(binary.BigEndian.Uint16(dat))
return sizeof16, nil
}
type I32be int32
func (I32be) BinaryStaticSize() int { return sizeof32 }
func (x I32be) MarshalBinary() ([]byte, error) {
var buf [sizeof32]byte
binary.BigEndian.PutUint32(buf[:], uint32(x))
return buf[:], nil
}
func (x *I32be) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof32); err != nil {
return 0, err
}
*x = I32be(binary.BigEndian.Uint32(dat))
return sizeof32, nil
}
type I64be int64
func (I64be) BinaryStaticSize() int { return sizeof64 }
func (x I64be) MarshalBinary() ([]byte, error) {
var buf [sizeof64]byte
binary.BigEndian.PutUint64(buf[:], uint64(x))
return buf[:], nil
}
func (x *I64be) UnmarshalBinary(dat []byte) (int, error) {
if err := binutil.NeedNBytes(dat, sizeof64); err != nil {
return 0, err
}
*x = I64be(binary.BigEndian.Uint64(dat))
return sizeof64, nil
}
|