-
Notifications
You must be signed in to change notification settings - Fork 37
/
unpacker.go
301 lines (259 loc) · 8.39 KB
/
unpacker.go
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package binpacker
import (
"encoding/binary"
"fmt"
"io"
"math"
"unsafe"
)
// Unpacker helps you unpack binary data from an io.Reader.
type Unpacker struct {
reader io.Reader
endian binary.ByteOrder
err error
}
// NewUnpacker returns a *Unpacker which hold an io.Reader. User must provide the byte order explicitly.
func NewUnpacker(endian binary.ByteOrder, reader io.Reader) *Unpacker {
return &Unpacker{
reader: reader,
endian: endian,
}
}
// Error returns an error if any errors exists
func (u *Unpacker) Error() error {
return u.err
}
// ShiftByte fetch the first byte in io.Reader. Returns a byte and an error if
// exists.
func (u *Unpacker) ShiftByte() (byte, error) {
buffer := make([]byte, 1)
_, err := io.ReadFull(u.reader, buffer)
return buffer[0], err
}
// FetchByte fetch the first byte in io.Reader and set to b.
func (u *Unpacker) FetchByte(b *byte) *Unpacker {
return u.errFilter(func() {
*b, u.err = u.ShiftByte()
})
}
// ShiftBytes fetch n bytes in io.Reader. Returns a byte array and an error if
// exists.
func (u *Unpacker) ShiftBytes(_n uint64) ([]byte, error) {
buf := make([]byte, _n)
_, err := io.ReadFull(u.reader, buf)
return buf, err
}
// FetchBytes read n bytes and set to bytes.
func (u *Unpacker) FetchBytes(n uint64, bytes *[]byte) *Unpacker {
return u.errFilter(func() {
*bytes, u.err = u.ShiftBytes(n)
})
}
// ShiftUint8 fetch 1 byte in io.Reader and covert it to uint8
func (u *Unpacker) ShiftUint8() (uint8, error) {
buffer := make([]byte, 1)
_, err := io.ReadFull(u.reader, buffer)
return buffer[0], err
}
// FetchUint8 read 1 byte, convert it to uint8 and set it to i.
func (u *Unpacker) FetchUint8(i *uint8) *Unpacker {
return u.errFilter(func() {
*i, u.err = u.ShiftUint8()
})
}
// ShiftUint16 fetch 2 bytes in io.Reader and convert it to uint16.
func (u *Unpacker) ShiftUint16() (uint16, error) {
buffer := make([]byte, 2)
if _, err := io.ReadFull(u.reader, buffer); err != nil {
return 0, err
}
return u.endian.Uint16(buffer), nil
}
// FetchUint16 read 2 bytes, convert it to uint16 and set it to i.
func (u *Unpacker) FetchUint16(i *uint16) *Unpacker {
return u.errFilter(func() {
*i, u.err = u.ShiftUint16()
})
}
// ShiftInt16 fetch 2 bytes in io.Reader and convert it to int16.
func (u *Unpacker) ShiftInt16() (int16, error) {
i, err := u.ShiftUint16()
return int16(i), err
}
// FetchInt16 read 2 bytes, convert it to int16 and set it to i.
func (u *Unpacker) FetchInt16(i *int16) *Unpacker {
return u.FetchUint16((*uint16)(unsafe.Pointer(i)))
}
// ShiftUint32 fetch 4 bytes in io.Reader and convert it to uint32.
func (u *Unpacker) ShiftUint32() (uint32, error) {
buffer := make([]byte, 4)
if _, err := io.ReadFull(u.reader, buffer); err != nil {
return 0, err
}
return u.endian.Uint32(buffer), nil
}
// ShiftInt32 fetch 4 bytes in io.Reader and convert it to int32.
func (u *Unpacker) ShiftInt32() (int32, error) {
i, err := u.ShiftUint32()
return int32(i), err
}
// FetchUint32 read 4 bytes, convert it to uint32 and set it to i.
func (u *Unpacker) FetchUint32(i *uint32) *Unpacker {
return u.errFilter(func() {
*i, u.err = u.ShiftUint32()
})
}
// FetchInt32 read 4 bytes, convert it to int32 and set it to i.
func (u *Unpacker) FetchInt32(i *int32) *Unpacker {
return u.FetchUint32((*uint32)(unsafe.Pointer(i)))
}
// ShiftUint64 fetch 8 bytes in io.Reader and convert it to uint64.
func (u *Unpacker) ShiftUint64() (uint64, error) {
buffer := make([]byte, 8)
if _, err := io.ReadFull(u.reader, buffer); err != nil {
return 0, err
}
return u.endian.Uint64(buffer), nil
}
// ShiftInt64 fetch 8 bytes in io.Reader and convert it to int64.
func (u *Unpacker) ShiftInt64() (int64, error) {
i, err := u.ShiftUint64()
return int64(i), err
}
// FetchUint64 read 8 bytes, convert it to uint64 and set it to i.
func (u *Unpacker) FetchUint64(i *uint64) *Unpacker {
return u.errFilter(func() {
*i, u.err = u.ShiftUint64()
})
}
// FetchInt64 read 8 bytes, convert it to int64 and set it to i.
func (u *Unpacker) FetchInt64(i *int64) *Unpacker {
return u.FetchUint64((*uint64)(unsafe.Pointer(i)))
}
// ShiftFloat32 fetch 4 bytes in io.Reader and convert it to float32.
func (u *Unpacker) ShiftFloat32() (float32, error) {
buffer := make([]byte, 4)
if _, err := io.ReadFull(u.reader, buffer); err != nil {
return 0, err
}
return math.Float32frombits(u.endian.Uint32(buffer)), nil
}
// ShiftFloat64 fetch 8 bytes in io.Reader and convert it to float64.
func (u *Unpacker) ShiftFloat64() (float64, error) {
buffer := make([]byte, 8)
if _, err := io.ReadFull(u.reader, buffer); err != nil {
return 0, err
}
return math.Float64frombits(u.endian.Uint64(buffer)), nil
}
// FetchFloat32 read 4 bytes, convert it to float32 and set it to i.
func (u *Unpacker) FetchFloat32(i *float32) *Unpacker {
return u.errFilter(func() {
*i, u.err = u.ShiftFloat32()
})
}
// FetchFloat64 read 8 bytes, convert it to float64 and set it to i.
func (u *Unpacker) FetchFloat64(i *float64) *Unpacker {
return u.errFilter(func() {
*i, u.err = u.ShiftFloat64()
})
}
// ShiftString fetch n bytes, convert it to string. Returns string and an error.
func (u *Unpacker) ShiftString(n uint64) (string, error) {
buffer := make([]byte, n)
if _, err := io.ReadFull(u.reader, buffer); err != nil {
return "", err
}
return string(buffer), nil
}
// FetchString read n bytes, convert it to string and set t to s.
func (u *Unpacker) FetchString(n uint64, s *string) *Unpacker {
return u.errFilter(func() {
*s, u.err = u.ShiftString(n)
})
}
// StringWithUint16Prefix read 2 bytes as string length, then read N bytes,
// convert it to string and set it to s.
func (u *Unpacker) StringWithUint16Prefix(s *string) *Unpacker {
return u.errFilter(func() {
var n uint16
n, u.err = u.ShiftUint16()
u.FetchString(uint64(n), s)
})
}
// StringWithUint32Prefix read 4 bytes as string length, then read N bytes,
// convert it to string and set it to s.
func (u *Unpacker) StringWithUint32Prefix(s *string) *Unpacker {
return u.errFilter(func() {
var n uint32
n, u.err = u.ShiftUint32()
u.FetchString(uint64(n), s)
})
}
// StringWithUint64Prefix read 8 bytes as string length, then read N bytes,
// convert it to string and set it to s.
func (u *Unpacker) StringWithUint64Prefix(s *string) *Unpacker {
return u.errFilter(func() {
var n uint64
n, u.err = u.ShiftUint64()
u.FetchString(n, s)
})
}
// BytesWithUint16Prefix read 2 bytes as bytes length, then read N bytes and set
// it to bytes.
func (u *Unpacker) BytesWithUint16Prefix(bytes *[]byte) *Unpacker {
return u.errFilter(func() {
var n uint16
n, u.err = u.ShiftUint16()
u.FetchBytes(uint64(n), bytes)
})
}
// BytesWithUint32Prefix read 4 bytes as bytes length, then read N bytes and set
// it to bytes.
func (u *Unpacker) BytesWithUint32Prefix(bytes *[]byte) *Unpacker {
return u.errFilter(func() {
var n uint32
n, u.err = u.ShiftUint32()
u.FetchBytes(uint64(n), bytes)
})
}
// BytesWithUint64Prefix read 8 bytes as bytes length, then read N bytes and set
// it to bytes.
func (u *Unpacker) BytesWithUint64Prefix(bytes *[]byte) *Unpacker {
return u.errFilter(func() {
var n uint64
n, u.err = u.ShiftUint64()
u.FetchBytes(n, bytes)
})
}
func (u *Unpacker) errFilter(f func()) *Unpacker {
if u.err == nil {
f()
}
return u
}
// Deprecated functions - see https://github.com/zhuangsirui/binpacker/issues/5
func (u *Unpacker) StringWithUint16Perfix(s *string) *Unpacker {
fmt.Println("StringWithUint16Perfix deprecated - use StringWithUint16Prefix instead")
return u.StringWithUint16Prefix(s)
}
func (u *Unpacker) StringWithUint32Perfix(s *string) *Unpacker {
fmt.Println("StringWithUint32Perfix deprecated - use StringWithUint32Prefix instead")
return u.StringWithUint32Prefix(s)
}
func (u *Unpacker) StringWithUint64Perfix(s *string) *Unpacker {
fmt.Println("StringWithUint64Perfix deprecated - use StringWithUint64Prefix instead")
return u.StringWithUint64Prefix(s)
}
func (u *Unpacker) BytesWithUint16Perfix(bytes *[]byte) *Unpacker {
fmt.Println("BytesWithUint16Perfix deprecated - use BytesWithUint16Prefix instead")
return u.BytesWithUint16Prefix(bytes)
}
func (u *Unpacker) BytesWithUint32Perfix(bytes *[]byte) *Unpacker {
fmt.Println("BytesWithUint32Perfix deprecated - use BytesWithUint32Prefix instead")
return u.BytesWithUint32Prefix(bytes)
}
func (u *Unpacker) BytesWithUint64Perfix(bytes *[]byte) *Unpacker {
fmt.Println("BytesWithUint64Perfix deprecated - use BytesWithUint64Prefix instead")
return u.BytesWithUint64Prefix(bytes)
}