forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 7
/
net.go
183 lines (162 loc) · 4.16 KB
/
net.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
package streambuf
// read integers in network byte order
import (
"github.com/elastic/beats/libbeat/common"
)
// Parse 8bit binary value from Buffer.
func (b *Buffer) ReadNetUint8() (uint8, error) {
if b.Failed() {
return 0, b.err
}
tmp := b.data[b.mark:]
if err := b.Advance(1); err != nil {
return 0, err
}
value := tmp[0]
return value, nil
}
// Write 8bit binary value to Buffer.
func (b *Buffer) WriteNetUint8(u uint8) error {
return b.Append([]byte{u})
}
// Parse 8bit binary value from the buffer at index. Will not advance the read
// buffer
func (b *Buffer) ReadNetUint8At(index int) (uint8, error) {
if b.Failed() {
return 0, b.err
}
if !b.Avail(1 + index) {
return 0, b.bufferEndError()
}
return b.data[index+b.mark], nil
}
// Write 8bit binary value at index.
func (b *Buffer) WriteNetUint8At(u uint8, index int) error {
if b.err != nil {
return b.err
}
b.sliceAt(index, 1)[0] = u
return nil
}
// Parse 16bit binary value in network byte order from Buffer
// (converted to Host order).
func (b *Buffer) ReadNetUint16() (uint16, error) {
if b.Failed() {
return 0, b.err
}
tmp := b.data[b.mark:]
if err := b.Advance(2); err != nil {
return 0, err
}
value := common.BytesNtohs(tmp)
return value, nil
}
// Write 16bit binary value in network byte order to buffer.
func (b *Buffer) WriteNetUint16(u uint16) error {
return b.WriteNetUint16At(u, b.available)
}
// Parse 16bit binary value from the buffer at index. Will not advance the read
// buffer
func (b *Buffer) ReadNetUint16At(index int) (uint16, error) {
if b.Failed() {
return 0, b.err
}
if !b.Avail(2 + index) {
return 0, b.bufferEndError()
}
return common.BytesNtohs(b.data[index+b.mark:]), nil
}
// Write 16bit binary value at index in network byte order to buffer.
func (b *Buffer) WriteNetUint16At(u uint16, index int) error {
if b.err != nil {
return b.err
}
tmp := b.sliceAt(index, 2)
tmp[0] = uint8(u >> 8)
tmp[1] = uint8(u)
return nil
}
// Parse 32bit binary value in network byte order from Buffer
// (converted to Host order).
func (b *Buffer) ReadNetUint32() (uint32, error) {
if b.Failed() {
return 0, b.err
}
tmp := b.data[b.mark:]
if err := b.Advance(4); err != nil {
return 0, err
}
value := common.BytesNtohl(tmp)
return value, nil
}
// Write 32bit binary value in network byte order to buffer.
func (b *Buffer) WriteNetUint32(u uint32) error {
return b.WriteNetUint32At(u, b.available)
}
// Parse 32bit binary value from the buffer at index. Will not advance the read
// buffer
func (b *Buffer) ReadNetUint32At(index int) (uint32, error) {
if b.Failed() {
return 0, b.err
}
if !b.Avail(4 + index) {
return 0, b.bufferEndError()
}
return common.BytesNtohl(b.data[index+b.mark:]), nil
}
// Write 32bit binary value at index in network byte order to buffer.
func (b *Buffer) WriteNetUint32At(u uint32, index int) error {
if b.err != nil {
return b.err
}
tmp := b.sliceAt(index, 4)
tmp[0] = uint8(u >> 24)
tmp[1] = uint8(u >> 16)
tmp[2] = uint8(u >> 8)
tmp[3] = uint8(u)
return nil
}
// Parse 64bit binary value in network byte order from Buffer
// (converted to Host order).
func (b *Buffer) ReadNetUint64() (uint64, error) {
if b.Failed() {
return 0, b.err
}
tmp := b.data[b.mark:]
if err := b.Advance(8); err != nil {
return 0, err
}
value := common.BytesNtohll(tmp)
return value, nil
}
// Write 64bit binary value in network byte order to buffer.
func (b *Buffer) WriteNetUint64(u uint64) error {
return b.WriteNetUint64At(u, b.available)
}
// Parse 64bit binary value from the buffer at index. Will not advance the read
// buffer
func (b *Buffer) ReadNetUint64At(index int) (uint64, error) {
if b.Failed() {
return 0, b.err
}
if !b.Avail(8 + index) {
return 0, b.bufferEndError()
}
return common.BytesNtohll(b.data[index+b.mark:]), nil
}
// Write 64bit binary value at index in network byte order to buffer.
func (b *Buffer) WriteNetUint64At(u uint64, index int) error {
if b.err != nil {
return b.err
}
tmp := b.sliceAt(index, 8)
tmp[0] = uint8(u >> 56)
tmp[1] = uint8(u >> 48)
tmp[2] = uint8(u >> 40)
tmp[3] = uint8(u >> 32)
tmp[4] = uint8(u >> 24)
tmp[5] = uint8(u >> 16)
tmp[6] = uint8(u >> 8)
tmp[7] = uint8(u)
return nil
}