forked from criteo/haproxy-spoe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoding.go
342 lines (286 loc) · 6.38 KB
/
encoding.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
package spoe
import (
"encoding/binary"
"fmt"
"net"
"github.com/pkg/errors"
)
type dataType byte
const (
dataTypeNull dataType = 0
dataTypeBool dataType = 1
dataTypeInt32 dataType = 2
dataTypeUInt32 dataType = 3
dataTypeInt64 dataType = 4
dataTypeUInt64 dataType = 5
dataTypeIPV4 dataType = 6
dataTypeIPV6 dataType = 7
dataTypeString dataType = 8
dataTypeBinary dataType = 9
)
const (
dataTypeMask byte = 0x0F
dataFlagMask byte = 0xF0
dataFlagTrue byte = 0x10
)
func decodeUint32(b []byte) (uint32, int, error) {
// read the frame length
if len(b) < 4 {
return 0, 0, fmt.Errorf("decode uint32: need at least 4 bytes, got %d", len(b))
}
v := binary.BigEndian.Uint32(b)
return v, 4, nil
}
func decodeVarint(b []byte) (int, int, error) {
if len(b) == 0 {
return 0, 0, fmt.Errorf("decode varint: unterminated sequence")
}
val := int(b[0])
off := 1
if val < 240 {
return val, 1, nil
}
r := uint(4)
for {
if off > len(b)-1 {
return 0, 0, fmt.Errorf("decode varint: unterminated sequence")
}
v := int(b[off])
val += v << r
off++
r += 7
if v < 128 {
break
}
}
return val, off, nil
}
func encodeVarint(b []byte, i int) (int, error) {
if len(b) == 0 {
return 0, fmt.Errorf("encode varint: insufficient space in buffer")
}
if i < 240 {
b[0] = byte(i)
return 1, nil
}
n := 0
b[n] = byte(i) | 240
n++
i = (i - 240) >> 4
for i >= 128 {
if n > len(b)-1 {
return 0, fmt.Errorf("encode varint: insufficient space in buffer")
}
b[n] = byte(i) | 128
n++
i = (i - 128) >> 7
}
if n > len(b)-1 {
return 0, fmt.Errorf("encode varint: insufficient space in buffer")
}
b[n] = byte(i)
n++
return n, nil
}
func decodeBytes(b []byte) ([]byte, int, error) {
l, off, err := decodeVarint(b)
if err != nil {
return nil, 0, errors.Wrap(err, "decode bytes")
}
if len(b) < l+off {
return nil, 0, fmt.Errorf("decode bytes: unterminated sequence")
}
return b[off : off+l], off + l, nil
}
func encodeBytes(b []byte, v []byte) (int, error) {
l := len(v)
n, err := encodeVarint(b, l)
if err != nil {
return 0, err
}
if l+n > len(b) {
return 0, fmt.Errorf("encode bytes: insufficient space in buffer")
}
copy(b[n:], v)
return n + l, nil
}
func decodeIPV4(b []byte) (net.IP, int, error) {
if len(b) < net.IPv4len {
return nil, 0, fmt.Errorf("decode ipv4: unterminated sequence")
}
return net.IP(b[:net.IPv4len]), net.IPv4len, nil
}
func encodeIPV4(b []byte, ip net.IP) (int, error) {
if len(b) < net.IPv4len {
return 0, fmt.Errorf("decode ipv4: unterminated sequence")
}
copy(b, ip)
return net.IPv4len, nil
}
func encodeIPV6(b []byte, ip net.IP) (int, error) {
if len(b) < net.IPv6len {
return 0, fmt.Errorf("decode ipv4: unterminated sequence")
}
copy(b, ip)
return net.IPv6len, nil
}
func decodeIPV6(b []byte) (net.IP, int, error) {
if len(b) < net.IPv6len {
return nil, 0, fmt.Errorf("decode ipv6: unterminated sequence")
}
return net.IP(b[:net.IPv6len]), net.IPv6len, nil
}
func decodeString(b []byte) (string, int, error) {
b, n, err := decodeBytes(b)
return string(b), n, err
}
func encodeString(b []byte, v string) (int, error) {
return encodeBytes(b, []byte(v))
}
func decodeKV(b []byte) (string, interface{}, int, error) {
off := 0
name, n, err := decodeString(b[off:])
if err != nil {
return "", nil, 0, errors.Wrap(err, "decode k/v")
}
off += n
dbyte := b[off]
dtype := dataType(dbyte & dataTypeMask)
off++
var value interface{}
switch dtype {
case dataTypeNull:
// noop
case dataTypeBool:
value = dbyte&dataFlagTrue > 0
case dataTypeInt32, dataTypeInt64:
v, n, err := decodeVarint(b[off:])
if err != nil {
return "", nil, 0, errors.Wrap(err, "decode k/v")
}
off += n
value = int(v)
case dataTypeUInt32, dataTypeUInt64:
v, n, err := decodeVarint(b[off:])
if err != nil {
return "", nil, 0, errors.Wrap(err, "decode k/v")
}
off += n
value = uint(v)
case dataTypeIPV4:
v, n, err := decodeIPV4(b[off:])
if err != nil {
return "", nil, 0, errors.Wrap(err, "decode k/v")
}
off += n
value = v
case dataTypeIPV6:
v, n, err := decodeIPV6(b[off:])
if err != nil {
return "", nil, 0, errors.Wrap(err, "decode k/v")
}
off += n
value = v
case dataTypeString:
v, n, err := decodeString(b[off:])
if err != nil {
return "", nil, 0, errors.Wrap(err, "decode k/v")
}
off += n
value = v
case dataTypeBinary:
v, n, err := decodeBytes(b[off:])
if err != nil {
return "", nil, 0, errors.Wrap(err, "decode k/v")
}
off += n
value = v
default:
return "", nil, 0, fmt.Errorf("decode k/v: unknown data type %x", dtype)
}
return name, value, off, nil
}
func decodeKVs(b []byte, count int) (map[string]interface{}, int, error) {
ml := count
if ml == -1 {
ml = 1
}
res := make(map[string]interface{}, ml)
off := 0
for off < len(b) && (count == -1 || len(res) < count) {
name, value, n, err := decodeKV(b[off:])
if err != nil {
return nil, 0, err
}
off += n
res[name] = value
}
return res, off, nil
}
func encodeKV(b []byte, name string, v interface{}) (int, error) {
n, err := encodeString(b, name)
if err != nil {
return 0, errors.Wrapf(err, "encode k/v (%s): %s", name, err)
}
if len(b) == n {
return 0, fmt.Errorf("encode k/v (%s): insufficient space", name)
}
var m int
switch val := v.(type) {
case int:
b[n] = byte(dataTypeInt64)
n++
m, err = encodeVarint(b[n:], int(val))
case int64:
b[n] = byte(dataTypeInt64)
n++
m, err = encodeVarint(b[n:], int(val))
case uint:
b[n] = byte(dataTypeUInt64)
n++
m, err = encodeVarint(b[n:], int(val))
case uint64:
b[n] = byte(dataTypeUInt64)
n++
m, err = encodeVarint(b[n:], int(val))
case int32:
b[n] = byte(dataTypeInt32)
n++
m, err = encodeVarint(b[n:], int(val))
case uint32:
b[n] = byte(dataTypeUInt32)
n++
m, err = encodeVarint(b[n:], int(val))
case string:
b[n] = byte(dataTypeString)
n++
m, err = encodeString(b[n:], val)
case []byte:
b[n] = byte(dataTypeBinary)
n++
m, err = encodeBytes(b[n:], val)
case net.IP:
if v4 := val.To4(); len(v4) > 0 {
b[n] = byte(dataTypeIPV4)
n++
m, err = encodeIPV4(b[n:], v4)
} else {
b[n] = byte(dataTypeIPV6)
n++
m, err = encodeIPV6(b[n:], val)
}
case bool:
v := byte(dataTypeBool)
if val {
v |= dataFlagTrue
}
b[n] = v
n++
default:
return 0, fmt.Errorf("encode k/v (%s): type %T is not handled", name, v)
}
if err != nil {
return 0, err
}
return n + m, nil
}