-
Notifications
You must be signed in to change notification settings - Fork 649
/
packing.go
522 lines (452 loc) · 12.5 KB
/
packing.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package wrappers
import (
"crypto/x509"
"encoding/binary"
"errors"
"math"
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/ips"
)
const (
MaxStringLen = math.MaxUint16
// ByteLen is the number of bytes per byte...
ByteLen = 1
// ShortLen is the number of bytes per short
ShortLen = 2
// IntLen is the number of bytes per int
IntLen = 4
// LongLen is the number of bytes per long
LongLen = 8
// BoolLen is the number of bytes per bool
BoolLen = 1
// IPLen is the number of bytes per IP
IPLen = 16 + ShortLen
)
var (
errBadLength = errors.New("packer has insufficient length for input")
errNegativeOffset = errors.New("negative offset")
errInvalidInput = errors.New("input does not match expected format")
errBadType = errors.New("wrong type passed")
errBadBool = errors.New("unexpected value when unpacking bool")
)
// Packer packs and unpacks a byte array from/to standard values
type Packer struct {
Errs
// The largest allowed size of expanding the byte array
MaxSize int
// The current byte array
Bytes []byte
// The offset that is being written to in the byte array
Offset int
}
// CheckSpace requires that there is at least [bytes] of write space left in the
// byte array. If this is not true, an error is added to the packer
func (p *Packer) CheckSpace(bytes int) {
switch {
case p.Offset < 0:
p.Add(errNegativeOffset)
case bytes < 0:
p.Add(errInvalidInput)
case len(p.Bytes)-p.Offset < bytes:
p.Add(errBadLength)
}
}
// Expand ensures that there is [bytes] bytes left of space in the byte slice.
// If this is not allowed due to the maximum size, an error is added to the packer
// In order to understand this code, its important to understand the difference
// between a slice's length and its capacity.
func (p *Packer) Expand(bytes int) {
neededSize := bytes + p.Offset // Need byte slice's length to be at least [neededSize]
switch {
case neededSize <= len(p.Bytes): // Byte slice has sufficient length already
return
case neededSize > p.MaxSize: // Lengthening the byte slice would cause it to grow too large
p.Err = errBadLength
return
case neededSize <= cap(p.Bytes): // Byte slice has sufficient capacity to lengthen it without mem alloc
p.Bytes = p.Bytes[:neededSize]
return
default: // Add capacity/length to byte slice
p.Bytes = append(p.Bytes[:cap(p.Bytes)], make([]byte, neededSize-cap(p.Bytes))...)
}
}
// PackByte append a byte to the byte array
func (p *Packer) PackByte(val byte) {
p.Expand(ByteLen)
if p.Errored() {
return
}
p.Bytes[p.Offset] = val
p.Offset++
}
// UnpackByte unpack a byte from the byte array
func (p *Packer) UnpackByte() byte {
p.CheckSpace(ByteLen)
if p.Errored() {
return 0
}
val := p.Bytes[p.Offset]
p.Offset++
return val
}
// PackShort append a short to the byte array
func (p *Packer) PackShort(val uint16) {
p.Expand(ShortLen)
if p.Errored() {
return
}
binary.BigEndian.PutUint16(p.Bytes[p.Offset:], val)
p.Offset += ShortLen
}
// UnpackShort unpack a short from the byte array
func (p *Packer) UnpackShort() uint16 {
p.CheckSpace(ShortLen)
if p.Errored() {
return 0
}
val := binary.BigEndian.Uint16(p.Bytes[p.Offset:])
p.Offset += ShortLen
return val
}
// PackInt append an int to the byte array
func (p *Packer) PackInt(val uint32) {
p.Expand(IntLen)
if p.Errored() {
return
}
binary.BigEndian.PutUint32(p.Bytes[p.Offset:], val)
p.Offset += IntLen
}
// UnpackInt unpack an int from the byte array
func (p *Packer) UnpackInt() uint32 {
p.CheckSpace(IntLen)
if p.Errored() {
return 0
}
val := binary.BigEndian.Uint32(p.Bytes[p.Offset:])
p.Offset += IntLen
return val
}
// PackLong append a long to the byte array
func (p *Packer) PackLong(val uint64) {
p.Expand(LongLen)
if p.Errored() {
return
}
binary.BigEndian.PutUint64(p.Bytes[p.Offset:], val)
p.Offset += LongLen
}
// UnpackLong unpack a long from the byte array
func (p *Packer) UnpackLong() uint64 {
p.CheckSpace(LongLen)
if p.Errored() {
return 0
}
val := binary.BigEndian.Uint64(p.Bytes[p.Offset:])
p.Offset += LongLen
return val
}
// PackBool packs a bool into the byte array
func (p *Packer) PackBool(b bool) {
if b {
p.PackByte(1)
} else {
p.PackByte(0)
}
}
// UnpackBool unpacks a bool from the byte array
func (p *Packer) UnpackBool() bool {
b := p.UnpackByte()
switch b {
case 0:
return false
case 1:
return true
default:
p.Add(errBadBool)
return false
}
}
// PackFixedBytes append a byte slice, with no length descriptor to the byte
// array
func (p *Packer) PackFixedBytes(bytes []byte) {
p.Expand(len(bytes))
if p.Errored() {
return
}
copy(p.Bytes[p.Offset:], bytes)
p.Offset += len(bytes)
}
// UnpackFixedBytes unpack a byte slice, with no length descriptor from the byte
// array
func (p *Packer) UnpackFixedBytes(size int) []byte {
p.CheckSpace(size)
if p.Errored() {
return nil
}
bytes := p.Bytes[p.Offset : p.Offset+size]
p.Offset += size
return bytes
}
// PackBytes append a byte slice to the byte array
func (p *Packer) PackBytes(bytes []byte) {
p.PackInt(uint32(len(bytes)))
p.PackFixedBytes(bytes)
}
// UnpackBytes unpack a byte slice from the byte array
func (p *Packer) UnpackBytes() []byte {
size := p.UnpackInt()
return p.UnpackFixedBytes(int(size))
}
// PackFixedByteSlices append a byte slice slice to the byte array
func (p *Packer) PackFixedByteSlices(byteSlices [][]byte) {
p.PackInt(uint32(len(byteSlices)))
for _, bytes := range byteSlices {
p.PackFixedBytes(bytes)
}
}
// UnpackFixedByteSlices returns a byte slice slice from the byte array.
// Each byte slice has the specified size. The number of byte slices is
// read from the byte array.
func (p *Packer) UnpackFixedByteSlices(size int) [][]byte {
sliceSize := p.UnpackInt()
bytes := [][]byte(nil)
for i := uint32(0); i < sliceSize && !p.Errored(); i++ {
bytes = append(bytes, p.UnpackFixedBytes(size))
}
return bytes
}
// Pack2DByteSlice append a 2D byte slice to the byte array
func (p *Packer) Pack2DByteSlice(byteSlices [][]byte) {
p.PackInt(uint32(len(byteSlices)))
for _, bytes := range byteSlices {
p.PackBytes(bytes)
}
}
// Unpack2DByteSlice returns a 2D byte slice from the byte array.
func (p *Packer) Unpack2DByteSlice() [][]byte {
sliceSize := p.UnpackInt()
bytes := [][]byte(nil)
for i := uint32(0); i < sliceSize && !p.Errored(); i++ {
bytes = append(bytes, p.UnpackBytes())
}
return bytes
}
// PackStr append a string to the byte array
func (p *Packer) PackStr(str string) {
strSize := len(str)
if strSize > MaxStringLen {
p.Add(errInvalidInput)
return
}
p.PackShort(uint16(strSize))
p.PackFixedBytes([]byte(str))
}
// UnpackStr unpacks a string from the byte array
func (p *Packer) UnpackStr() string {
strSize := p.UnpackShort()
return string(p.UnpackFixedBytes(int(strSize)))
}
// PackIP packs an ip port pair to the byte array
func (p *Packer) PackIP(ip ips.IPPort) {
p.PackFixedBytes(ip.IP.To16())
p.PackShort(ip.Port)
}
// UnpackIP unpacks an ip port pair from the byte array
func (p *Packer) UnpackIP() ips.IPPort {
ip := p.UnpackFixedBytes(16)
port := p.UnpackShort()
return ips.IPPort{
IP: ip,
Port: port,
}
}
// PackIPs unpacks an ip port pair slice from the byte array
func (p *Packer) PackIPs(ips []ips.IPPort) {
p.PackInt(uint32(len(ips)))
for i := 0; i < len(ips) && !p.Errored(); i++ {
p.PackIP(ips[i])
}
}
// UnpackIPs unpacks an ip port pair slice from the byte array
func (p *Packer) UnpackIPs() []ips.IPPort {
sliceSize := p.UnpackInt()
ips := []ips.IPPort(nil)
for i := uint32(0); i < sliceSize && !p.Errored(); i++ {
ips = append(ips, p.UnpackIP())
}
return ips
}
// TryPackByte attempts to pack the value as a byte
func TryPackByte(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.(uint8); ok {
packer.PackByte(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpackByte attempts to unpack a value as a byte
func TryUnpackByte(packer *Packer) interface{} {
return packer.UnpackByte()
}
// TryPackInt attempts to pack the value as an int
func TryPackInt(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.(uint32); ok {
packer.PackInt(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpackInt attempts to unpack a value as an int
func TryUnpackInt(packer *Packer) interface{} {
return packer.UnpackInt()
}
// TryPackLong attempts to pack the value as a long
func TryPackLong(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.(uint64); ok {
packer.PackLong(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpackLong attempts to unpack a value as a long
func TryUnpackLong(packer *Packer) interface{} {
return packer.UnpackLong()
}
// TryPackHash attempts to pack the value as a 32-byte sequence
func TryPackHash(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.([]byte); ok {
packer.PackFixedBytes(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpackHash attempts to unpack the value as a 32-byte sequence
func TryUnpackHash(packer *Packer) interface{} {
return packer.UnpackFixedBytes(hashing.HashLen)
}
// TryPackHashes attempts to pack the value as a list of 32-byte sequences
func TryPackHashes(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.([][]byte); ok {
packer.PackFixedByteSlices(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpackHashes attempts to unpack the value as a list of 32-byte sequences
func TryUnpackHashes(packer *Packer) interface{} {
return packer.UnpackFixedByteSlices(hashing.HashLen)
}
// TryPackBytes attempts to pack the value as a list of bytes
func TryPackBytes(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.([]byte); ok {
packer.PackBytes(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpackBytes attempts to unpack the value as a list of bytes
func TryUnpackBytes(packer *Packer) interface{} {
return packer.UnpackBytes()
}
// TryPack2DBytes attempts to pack the value as a 2D byte slice
func TryPack2DBytes(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.([][]byte); ok {
packer.Pack2DByteSlice(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpack2DBytes attempts to unpack the value as a 2D byte slice
func TryUnpack2DBytes(packer *Packer) interface{} {
return packer.Unpack2DByteSlice()
}
// TryPackStr attempts to pack the value as a string
func TryPackStr(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.(string); ok {
packer.PackStr(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpackStr attempts to unpack the value as a string
func TryUnpackStr(packer *Packer) interface{} {
return packer.UnpackStr()
}
// TryPackIP attempts to pack the value as an ip port pair
func TryPackIP(packer *Packer, valIntf interface{}) {
if val, ok := valIntf.(ips.IPPort); ok {
packer.PackIP(val)
} else {
packer.Add(errBadType)
}
}
// TryUnpackIP attempts to unpack the value as an ip port pair
func TryUnpackIP(packer *Packer) interface{} {
return packer.UnpackIP()
}
func (p *Packer) PackX509Certificate(cert *x509.Certificate) {
p.PackBytes(cert.Raw)
}
func (p *Packer) UnpackX509Certificate() *x509.Certificate {
b := p.UnpackBytes()
cert, err := x509.ParseCertificate(b)
if err != nil {
p.Add(err)
return nil
}
return cert
}
func (p *Packer) PackClaimedIPPort(ipCert ips.ClaimedIPPort) {
p.PackX509Certificate(ipCert.Cert)
p.PackIP(ipCert.IPPort)
p.PackLong(ipCert.Timestamp)
p.PackBytes(ipCert.Signature)
}
func (p *Packer) UnpackClaimedIPPort() ips.ClaimedIPPort {
var ipCert ips.ClaimedIPPort
ipCert.Cert = p.UnpackX509Certificate()
ipCert.IPPort = p.UnpackIP()
ipCert.Timestamp = p.UnpackLong()
ipCert.Signature = p.UnpackBytes()
return ipCert
}
func TryPackClaimedIPPortList(packer *Packer, valIntf interface{}) {
if ipCertList, ok := valIntf.([]ips.ClaimedIPPort); ok {
packer.PackInt(uint32(len(ipCertList)))
for _, ipc := range ipCertList {
packer.PackClaimedIPPort(ipc)
}
} else {
packer.Add(errBadType)
}
}
func TryUnpackClaimedIPPortList(packer *Packer) interface{} {
sliceSize := packer.UnpackInt()
ips := []ips.ClaimedIPPort(nil)
for i := uint32(0); i < sliceSize && !packer.Errored(); i++ {
ips = append(ips, packer.UnpackClaimedIPPort())
}
return ips
}
func TryPackUint64Slice(p *Packer, valIntf interface{}) {
longList, ok := valIntf.([]uint64)
if !ok {
p.Add(errBadType)
return
}
p.PackInt(uint32(len(longList)))
for _, val := range longList {
p.PackLong(val)
}
}
func TryUnpackUint64Slice(p *Packer) interface{} {
sliceSize := p.UnpackInt()
res := []uint64(nil)
for i := uint32(0); i < sliceSize && !p.Errored(); i++ {
res = append(res, p.UnpackLong())
}
return res
}