forked from datashit/planet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
284 lines (252 loc) · 6.24 KB
/
packet.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
package planet
import (
"io"
"math"
)
// Buffer is a helper struct for serializing and deserializing as the caller
// does not need to externally manage where in the buffer they are currently reading
// or writing to.
type Buffer struct {
Buf []byte // the backing byte slice
Pos int // current position in read/write
}
// Creates a new Buffer with a backing byte slice of the provided size
func NewBuffer(size int) *Buffer {
b := &Buffer{}
b.Buf = make([]byte, size)
return b
}
// Creates a new Buffer using the original backing slice
func NewBufferFromRef(buf []byte) *Buffer {
b := &Buffer{}
b.Buf = buf
b.Pos = 0
return b
}
// Creates a new buffer from a byte slice
func NewBufferFromBytes(buf []byte) *Buffer {
b := &Buffer{}
b.Buf = make([]byte, len(buf))
copy(b.Buf, buf)
return b
}
// Returns a copy of Buffer
func (b *Buffer) Copy() *Buffer {
c := NewBuffer(len(b.Buf))
copy(c.Buf, b.Buf)
return c
}
// Gets the length of the backing byte slice
func (b *Buffer) Len() int {
return len(b.Buf)
}
// Returns the backing byte slice
func (b *Buffer) Bytes() []byte {
return b.Buf
}
// Resets the position back to beginning of buffer
func (b *Buffer) Reset() {
b.Pos = 0
}
// GetByte decodes a little-endian byte
func (b *Buffer) GetByte() (byte, error) {
return b.GetUint8()
}
// GetBytes returns a byte slice possibly smaller than length if bytes are not available from the
// reader.
func (b *Buffer) GetBytes(length int) ([]byte, error) {
bufferLength := len(b.Buf)
bufferWindow := b.Pos + length
if bufferLength < length {
return nil, io.EOF
}
if bufferWindow > bufferLength {
return nil, io.EOF
}
value := b.Buf[b.Pos:bufferWindow]
b.Pos += length
return value, nil
}
// GetUint8 decodes a little-endian uint8 from the buffer
func (b *Buffer) GetUint8() (uint8, error) {
buf, err := b.GetBytes(SizeUint8)
if err != nil {
return 0, nil
}
return uint8(buf[0]), nil
}
// GetUint16 decodes a little-endian uint16 from the buffer
func (b *Buffer) GetUint16() (uint16, error) {
var n uint16
buf, err := b.GetBytes(SizeUint16)
if err != nil {
return 0, nil
}
n |= uint16(buf[0])
n |= uint16(buf[1]) << 8
return n, nil
}
// GetUint32 decodes a little-endian uint32 from the buffer
func (b *Buffer) GetUint32() (uint32, error) {
var n uint32
buf, err := b.GetBytes(SizeUint32)
if err != nil {
return 0, nil
}
n |= uint32(buf[0])
n |= uint32(buf[1]) << 8
n |= uint32(buf[2]) << 16
n |= uint32(buf[3]) << 24
return n, nil
}
// GetUint64 decodes a little-endian uint64 from the buffer
func (b *Buffer) GetUint64() (uint64, error) {
var n uint64
buf, err := b.GetBytes(SizeUint64)
if err != nil {
return 0, nil
}
n |= uint64(buf[0])
n |= uint64(buf[1]) << 8
n |= uint64(buf[2]) << 16
n |= uint64(buf[3]) << 24
n |= uint64(buf[4]) << 32
n |= uint64(buf[5]) << 40
n |= uint64(buf[6]) << 48
n |= uint64(buf[7]) << 56
return n, nil
}
// GetInt8 decodes a little-endian int8 from the buffer
func (b *Buffer) GetInt8() (int8, error) {
buf, err := b.GetBytes(SizeInt8)
if err != nil {
return 0, nil
}
return int8(buf[0]), nil
}
// GetInt16 decodes a little-endian int16 from the buffer
func (b *Buffer) GetInt16() (int16, error) {
var n int16
buf, err := b.GetBytes(SizeInt16)
if err != nil {
return 0, nil
}
n |= int16(buf[0])
n |= int16(buf[1]) << 8
return n, nil
}
// GetInt32 decodes a little-endian int32 from the buffer
func (b *Buffer) GetInt32() (int32, error) {
var n int32
buf, err := b.GetBytes(SizeInt32)
if err != nil {
return 0, nil
}
n |= int32(buf[0])
n |= int32(buf[1]) << 8
n |= int32(buf[2]) << 16
n |= int32(buf[3]) << 24
return n, nil
}
// GetInt64 decodes a little-endian int64 from the buffer
func (b *Buffer) GetInt64() (int64, error) {
var n int64
buf, err := b.GetBytes(SizeInt64)
if err != nil {
return 0, nil
}
n |= int64(buf[0])
n |= int64(buf[1]) << 8
n |= int64(buf[2]) << 16
n |= int64(buf[3]) << 24
n |= int64(buf[4]) << 32
n |= int64(buf[5]) << 40
n |= int64(buf[6]) << 48
n |= int64(buf[7]) << 56
return n, nil
}
// WriteByte encodes a little-endian uint8 into the buffer.
func (b *Buffer) WriteByte(n byte) {
b.Buf[b.Pos] = n
b.Pos++
}
// WriteBytes encodes a little-endian byte slice into the buffer
func (b *Buffer) WriteBytes(src []byte) {
for i := 0; i < len(src); i += 1 {
b.WriteByte(src[i])
}
}
// WriteBytes encodes a little-endian byte slice into the buffer
func (b *Buffer) WriteBytesN(src []byte, length int) {
for i := 0; i < length; i += 1 {
b.WriteByte(src[i])
}
}
// WriteUint8 encodes a little-endian uint8 into the buffer.
func (b *Buffer) WriteUint8(n uint8) {
b.Buf[b.Pos] = byte(n)
b.Pos++
}
// WriteUint16 encodes a little-endian uint16 into the buffer.
func (b *Buffer) WriteUint16(n uint16) {
b.Buf[b.Pos] = byte(n)
b.Pos++
b.Buf[b.Pos] = byte(n >> 8)
b.Pos++
}
// WriteUint32 encodes a little-endian uint32 into the buffer.
func (b *Buffer) WriteUint32(n uint32) {
b.Buf[b.Pos] = byte(n)
b.Pos++
b.Buf[b.Pos] = byte(n >> 8)
b.Pos++
b.Buf[b.Pos] = byte(n >> 16)
b.Pos++
b.Buf[b.Pos] = byte(n >> 24)
b.Pos++
}
// WriteUint64 encodes a little-endian uint64 into the buffer.
func (b *Buffer) WriteUint64(n uint64) {
for i := uint(0); i < uint(SizeUint64); i++ {
b.Buf[b.Pos] = byte(n >> (i * 8))
b.Pos++
}
}
// WriteInt8 encodes a little-endian int8 into the buffer.
func (b *Buffer) WriteInt8(n int8) {
b.Buf[b.Pos] = byte(n)
b.Pos++
}
// WriteInt16 encodes a little-endian int16 into the buffer.
func (b *Buffer) WriteInt16(n int16) {
b.Buf[b.Pos] = byte(n)
b.Pos++
b.Buf[b.Pos] = byte(n >> 8)
b.Pos++
}
// WriteInt32 encodes a little-endian int32 into the buffer.
func (b *Buffer) WriteInt32(n int32) {
b.Buf[b.Pos] = byte(n)
b.Pos++
b.Buf[b.Pos] = byte(n >> 8)
b.Pos++
b.Buf[b.Pos] = byte(n >> 16)
b.Pos++
b.Buf[b.Pos] = byte(n >> 24)
b.Pos++
}
// WriteInt64 encodes a little-endian int64 into the buffer.
func (b *Buffer) WriteInt64(n int64) {
for i := uint(0); i < uint(SizeInt64); i++ {
b.Buf[b.Pos] = byte(n >> (i * 8))
b.Pos++
}
}
// WriteFloat32 encodes a little-endian float32 into the buffer.
func (b *Buffer) WriteFloat32(n float32) {
b.WriteUint32(math.Float32bits(n))
}
// WriteFloat64 encodes a little-endian float64 into the buffer.
func (b *Buffer) WriteFloat64(buf []byte, n float64) {
b.WriteUint64(math.Float64bits(n))
}