/
routines.go
69 lines (55 loc) · 1.35 KB
/
routines.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
package packet
import (
"encoding/binary"
)
// WriteToBuffer encode and send message into ring buffer
//func WriteToBuffer(msg Provider, to *goring.Buffer) (int, error) {
// expectedSize, err := msg.Size()
// if err != nil {
// return 0, err
// }
//
// if len(to.ExternalBuf) < expectedSize {
// to.ExternalBuf = make([]byte, expectedSize)
// }
//
// total, err := msg.Encode(to.ExternalBuf)
// if err != nil {
// return 0, err
// }
//
// return to.Send([][]byte{to.ExternalBuf[:total]})
//}
// ReadLPBytes read length prefixed bytes
func ReadLPBytes(buf []byte) ([]byte, int, error) {
if len(buf) < 2 {
return nil, 0, ErrInsufficientDataSize
}
var n int
total := 0
// Read length prefix
n = int(binary.BigEndian.Uint16(buf))
total += 2
// Check for malformed length-prefixed field
// if remaining space is less than length-prefixed size the packet seems to be broken
if len(buf[total:]) < n {
return nil, total, ErrInsufficientDataSize
}
total += n
return buf[2:total], total, nil
}
// WriteLPBytes write length prefixed bytes
func WriteLPBytes(buf []byte, b []byte) (int, error) {
total, n := 0, len(b)
if n > MaxLPString {
return 0, ErrInvalidLPStringSize
}
if len(buf) < 2+n {
return 2 + n, ErrInsufficientBufferSize
}
binary.BigEndian.PutUint16(buf, uint16(n))
total += 2
copy(buf[total:], b)
total += n
return total, nil
}