-
Notifications
You must be signed in to change notification settings - Fork 16
/
codec.go
361 lines (308 loc) · 8.11 KB
/
codec.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
package minq
import (
"bytes"
"fmt"
"io"
"reflect"
"runtime"
"strconv"
"strings"
"unicode"
)
const (
codecDefaultSize = ^uintptr(0)
)
func uintEncode(buf *bytes.Buffer, v reflect.Value, encodingSize uintptr) error {
size := v.Type().Size()
if encodingSize != codecDefaultSize {
if encodingSize > size {
return fmt.Errorf("Requested a length longer than the native type")
}
size = encodingSize
}
uintEncodeInt(buf, v.Uint(), size)
return nil
}
func uintEncodeInt(buf *bytes.Buffer, val uint64, size uintptr) {
// Now encode the low-order bytes of the value.
for b := size; b > 0; b -= 1 {
buf.WriteByte(byte(val >> ((b - 1) * 8)))
}
}
// isVarint determines if the field is a varint. This reads the mint/syntax tag
// for the field, but only supports a simple "varint".
func isVarint(f reflect.StructField) bool {
return f.Tag.Get("tls") == "varint"
}
func varintEncode(buf *bytes.Buffer, v uint64) {
switch {
case v < (uint64(1) << 6):
uintEncodeInt(buf, v, 1)
case v < (uint64(1) << 14):
uintEncodeInt(buf, v|(1<<14), 2)
case v < (uint64(1) << 30):
uintEncodeInt(buf, v|(2<<30), 4)
case v < (uint64(1) << 62):
uintEncodeInt(buf, v|(3<<62), 8)
default:
panic("varint value is too large")
}
}
func arrayEncode(buf *bytes.Buffer, v reflect.Value) error {
b := v.Bytes()
logf(logTypeCodec, "Encoding array length=%d", len(b))
buf.Write(b)
return nil
}
// Check to see if fields
func ignoreField(name string) bool {
return unicode.IsLower(rune(name[0]))
}
// Length specifications are of the form:
//
// lengthbits: "B:L1,L2,...LN
//
// where B is the rightmost bit of the length bits and
// L_n are the various lengths (in bytes) indicated by
// the bit values in sequence. N must be a power of 2
// and the right number of bytes is drawn to compute it.
type lengthSpec struct {
rightBit uint
numBits uint
values []int
}
func parseLengthSpecification(spec string) (*lengthSpec, error) {
spl := strings.Split(spec, ":")
assert(len(spl) == 2)
// Rightmost bit.
p, err := strconv.ParseUint(spl[0], 10, 8)
if err != nil {
return nil, err
}
bitr := uint(p)
vals := strings.Split(spl[1], ",")
// Figure out how many bits we need.
nvals := int(1)
var bits int
for bits = 1; bits <= 8; bits++ {
nvals <<= 1
if nvals == len(vals) {
break
}
}
assert(bits < 9)
// Now compute the values
valArr := make([]int, nvals)
for i, v := range vals {
valArr[i], err = strconv.Atoi(v)
if err != nil {
return nil, err
}
}
return &lengthSpec{
bitr,
uint(bits),
valArr,
}, nil
}
func computeLengthFromSpec(t byte, f reflect.StructField) uintptr {
st := f.Tag.Get("lengthbits")
if st == "" {
return codecDefaultSize
}
spec, err := parseLengthSpecification(st)
assert(err == nil)
mask := byte(0)
bit := uint(0)
for ; bit < spec.numBits; bit++ {
mask |= (1 << bit)
}
idx := int(t >> (spec.rightBit - 1) & mask)
return uintptr(spec.values[idx])
}
// Encode all the fields of a struct to a bytestring.
func encode(i interface{}) (ret []byte, err error) {
var buf bytes.Buffer
var res error
reflected := reflect.ValueOf(i).Elem()
fields := reflected.NumField()
for j := 0; j < fields; j += 1 {
field := reflected.Field(j)
tipe := reflected.Type().Field(j)
if ignoreField(tipe.Name) {
continue
}
logf(logTypeCodec, "Type name %s Kind=%v", tipe.Name, field.Kind())
switch field.Kind() {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
// Call the length overrider to tell us if we shoud be using a shorter
// encoding.
encodingSize := uintptr(codecDefaultSize)
lFunc, getLength := reflected.Type().MethodByName(tipe.Name + "__length")
logf(logTypeCodec, "Looking for length overrider for type %v", tipe.Name)
if getLength {
lengthResult := lFunc.Func.Call([]reflect.Value{reflect.ValueOf(i).Elem()})
encodingSize = uintptr(lengthResult[0].Uint())
logf(logTypeCodec, "Overriden length to %v", encodingSize)
}
if isVarint(tipe) {
if encodingSize != 0 {
varintEncode(&buf, field.Uint())
}
res = nil
break
}
res = uintEncode(&buf, field, encodingSize)
case reflect.Array, reflect.Slice:
res = arrayEncode(&buf, field)
default:
return nil, fmt.Errorf("Unknown type")
}
if res != nil {
return nil, res
}
}
ret = buf.Bytes()
logf(logTypeCodec, "Total encoded length = %v", len(ret))
return ret, nil
}
func uintDecodeIntBuf(val []byte) uint64 {
tmp := uint64(0)
for b := 0; b < len(val); b++ {
tmp = (tmp << 8) + uint64(val[b])
}
return tmp
}
func uintDecodeInt(r io.Reader, size uintptr) (uint64, error) {
val := make([]byte, size)
_, err := io.ReadFull(r, val)
if err != nil {
return 0, err
}
return uintDecodeIntBuf(val), nil
}
func uintDecode(r io.Reader, v reflect.Value, encodingSize uintptr) (uintptr, error) {
size := v.Type().Size()
if encodingSize != codecDefaultSize {
if encodingSize > size {
return 0, fmt.Errorf("Requested a length longer than the native type")
}
size = encodingSize
}
tmp, err := uintDecodeInt(r, size)
if err != nil {
return 0, err
}
v.SetUint(tmp)
return size, nil
}
func varintDecode(r io.Reader, v reflect.Value) (uintptr, error) {
p := make([]byte, 8)
_, err := r.Read(p[:1])
if err != nil {
return 0, err
}
value := uint64(p[0] & 0x3f)
extra := uintptr(1<<(p[0]>>6)) - 1
if extra > 0 {
tail, err := uintDecodeInt(r, extra)
if err != nil {
return 0, err
}
value = (value << (8 * extra)) | tail
}
v.SetUint(value)
return 1 + extra, nil
}
func encodeArgs(args ...interface{}) []byte {
var buf bytes.Buffer
var res error
for _, arg := range args {
reflected := reflect.ValueOf(arg)
switch reflected.Kind() {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
res = uintEncode(&buf, reflected, codecDefaultSize)
case reflect.Array, reflect.Slice:
res = arrayEncode(&buf, reflected)
default:
panic(fmt.Sprintf("Unknown type"))
}
if res != nil {
panic(fmt.Sprintf("Encoding error"))
}
}
return buf.Bytes()
}
func arrayDecode(r io.Reader, v reflect.Value, encodingSize uintptr) (uintptr, error) {
logf(logTypeCodec, "encodingSize = %v", encodingSize)
val := make([]byte, encodingSize)
logf(logTypeCodec, "Reading array of size %v", encodingSize)
// Go will return EOF if you try to read 0 bytes off a closed stream.
if encodingSize == 0 {
return 0, nil
}
_, err := io.ReadFull(r, val)
if err != nil {
return 0, err
}
v.SetBytes(val)
return encodingSize, nil
}
// Decode all the fields of a struct from a bytestring. Takes
// a pointer to the struct to fill in
func decode(i interface{}, data []byte) (uintptr, error) {
buf := bytes.NewReader(data)
var res error
reflected := reflect.ValueOf(i).Elem()
fields := reflected.NumField()
bytesread := uintptr(0)
for j := 0; j < fields; j++ {
br := uintptr(0)
field := reflected.Field(j)
tipe := reflected.Type().Field(j)
if ignoreField(tipe.Name) {
continue
}
// Call the length overrider to tell us if we should be using a shorter
// encoding.
encodingSize := uintptr(codecDefaultSize)
lFunc, getLength := reflected.Type().MethodByName(tipe.Name + "__length")
if getLength {
lengthResult := lFunc.Func.Call([]reflect.Value{reflect.ValueOf(i).Elem()})
encodingSize = uintptr(lengthResult[0].Uint())
logf(logTypeCodec, "Length overrider for %s returns %v", tipe.Name, encodingSize)
}
switch field.Kind() {
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if isVarint(tipe) && encodingSize != 0 {
br, res = varintDecode(buf, field)
} else {
br, res = uintDecode(buf, field, encodingSize)
}
case reflect.Array, reflect.Slice:
if encodingSize == codecDefaultSize {
encodingSize = uintptr(buf.Len())
}
br, res = arrayDecode(buf, field, encodingSize)
default:
return 0, fmt.Errorf("Unknown type")
}
if res != nil {
logf(logTypeCodec, "Error while reading field %v: %v", tipe.Name, res)
return bytesread, res
}
bytesread += br
}
return bytesread, nil
}
func backtrace() string {
bt := string("")
for i := 1; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
bt = fmt.Sprintf("%v: %d\n", file, line) + bt
}
return bt
}