-
Notifications
You must be signed in to change notification settings - Fork 12
/
typeddata.go
202 lines (172 loc) · 3.99 KB
/
typeddata.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
package typeddata
import (
"fmt"
"github.com/negasus/haproxy-spoe-go/varint"
"net"
"reflect"
"github.com/pkg/errors"
)
const (
// TypeNull const for TypedData type
TypeNull byte = 0
// TypeBoolean const for TypedData type
TypeBoolean byte = 1
// TypeInt32 const for TypedData type
TypeInt32 byte = 2
// TypeUInt32 const for TypedData type
TypeUInt32 byte = 3
// TypeInt64 const for TypedData type
TypeInt64 byte = 4
// TypeUInt64 const for TypedData type
TypeUInt64 byte = 5
// TypeIPv4 const for TypedData type
TypeIPv4 byte = 6
// TypeIPv6 const for TypedData type
TypeIPv6 byte = 7
// TypeString const for TypedData type
TypeString byte = 8
// TypeBinary const for TypedData type
TypeBinary byte = 9
)
//ErrEmptyBuffer describe error, if passed empty buffer for decoding
var ErrEmptyBuffer = errors.New("empty buffer for decode")
//ErrDecodingBufferTooSmall describe error for too small decoding buffer
var ErrDecodingBufferTooSmall = errors.New("decoding buffer too small")
//Encode variable to TypedData value
//returns filled buffer, count of bytes and error
func Encode(data interface{}, buf []byte) ([]byte, int, error) {
var n int
switch v := data.(type) {
case nil:
buf = append(buf, TypeNull)
return buf, 1, nil
case bool:
var b byte = 0x11
if !v {
b = 0x10
}
buf = append(buf, b)
return buf, 1, nil
case int32:
buf = append(buf, TypeInt32)
b := make([]byte, 8)
i := varint.PutUvarint(b, uint64(v))
buf = append(buf, b[:i]...)
return buf, i + 1, nil
case uint32:
buf = append(buf, TypeUInt32)
b := make([]byte, 8)
i := varint.PutUvarint(b, uint64(v))
buf = append(buf, b[:i]...)
return buf, i + 1, nil
case int:
buf = append(buf, TypeInt64)
b := make([]byte, 8)
i := varint.PutUvarint(b, uint64(v))
buf = append(buf, b[:i]...)
return buf, i + 1, nil
case int64:
buf = append(buf, TypeInt64)
b := make([]byte, 8)
i := varint.PutUvarint(b, uint64(v))
buf = append(buf, b[:i]...)
return buf, i + 1, nil
case uint:
buf = append(buf, TypeUInt64)
b := make([]byte, 8)
i := varint.PutUvarint(b, uint64(v))
buf = append(buf, b[:i]...)
return buf, i + 1, nil
case uint64:
buf = append(buf, TypeUInt64)
b := make([]byte, 8)
i := varint.PutUvarint(b, uint64(v))
buf = append(buf, b[:i]...)
return buf, i + 1, nil
case string:
n = 1
buf = append(buf, TypeString)
b := make([]byte, 8)
i := varint.PutUvarint(b, uint64(len(v)))
n += i
n += len(v)
buf = append(buf, b[:i]...)
buf = append(buf, v...)
return buf, n, nil
case []byte:
buf = append(buf, TypeBinary)
buf = append(buf, v...)
return buf, len(v) + 1, nil
}
return nil, 0, fmt.Errorf("type not supported for encode to TypedData: %s", reflect.TypeOf(data).String())
}
//Decode TypedData value
//Returns decoded variable, bytes count and error
func Decode(buf []byte) (data interface{}, n int, err error) {
if len(buf) == 0 {
err = ErrEmptyBuffer
return
}
f := buf[0] >> 4
t := buf[0] & 0x0F
buf = buf[1:]
n = 1
switch t {
case TypeNull:
return
case TypeBoolean:
data = f&0x01 > 0
return
case TypeInt32:
i, l := varint.Uvarint(buf)
n += l
data = int32(i)
return
case TypeUInt32:
i, l := varint.Uvarint(buf)
n += l
data = uint32(i)
return
case TypeInt64:
i, l := varint.Uvarint(buf)
n += l
data = int64(i)
return
case TypeUInt64:
i, l := varint.Uvarint(buf)
n += l
data = uint64(i)
return
case TypeIPv4:
data = net.IP(buf[:4])
n += 4
return
case TypeIPv6:
data = net.IP(buf[:16])
n += 16
return
case TypeString:
sLen, i := varint.Uvarint(buf)
n += i
buf = buf[i:]
if len(buf) < int(sLen) {
err = ErrDecodingBufferTooSmall
return
}
data = string(buf[:sLen])
n += int(sLen)
return
case TypeBinary:
dataLen, i := varint.Uvarint(buf)
n += i
buf = buf[i:]
if len(buf) < int(dataLen) {
err = ErrDecodingBufferTooSmall
return
}
data = buf[:dataLen]
n += int(dataLen)
return
}
return nil, n, fmt.Errorf("type %d not supported for decode from TypedData", t)
}