-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
134 lines (123 loc) · 2.61 KB
/
decode.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
package bare
import (
"errors"
"fmt"
"io"
"reflect"
)
func DecodeUInt(r io.ByteReader) (uint, error) {
res, shift := uint64(0), uint64(0)
done := false
for i := 0; !done; i++ {
b, err := r.ReadByte()
if err != nil {
return 0, err
}
done = (b & 0b1000_0000) == 0
if i == 9 && (!done || (b&0b0111_1110) != 0) {
return 0, errors.New("the max. precision of a BARE uint is 64 bits")
}
res = res | ((uint64(b) & 0b0111_1111) << shift)
shift += 7
}
return uint(res), nil
}
func DecodeInt(r io.ByteReader) (int, error) {
ures, err := DecodeUInt(r)
if err != nil {
return 0, err
}
isneg := (ures & 0b1) == 1
if isneg {
return -(int(ures) >> 1) - 1, nil
} else {
return int(ures >> 1), nil
}
}
func decodeValue(r io.ByteReader, dst reflect.Value) error {
switch dst.Kind() {
case reflect.Uint:
res, err := DecodeUInt(r)
if err != nil {
return err
}
dst.SetUint(uint64(res))
return nil
case reflect.Int:
res, err := DecodeInt(r)
if err != nil {
return err
}
dst.SetInt(int64(res))
return nil
case reflect.Array:
n := dst.Type().Len()
for i := range n {
if err := decodeValue(r, dst.Index(i)); err != nil {
return err
}
}
return nil
case reflect.Bool:
b, err := r.ReadByte()
if err != nil {
return err
}
if b != 0 && b != 1 {
return errors.New("expected zero or one for booleans")
}
dst.SetBool(b == 1)
return nil
case reflect.Slice:
n, err := DecodeUInt(r)
if err != nil {
return fmt.Errorf("expected a error: %w", err)
}
sliceVal := reflect.MakeSlice(dst.Type().Elem(), int(n), int(n))
for i := range int(n) {
if err := decodeValue(r, dst.Index(i)); err != nil {
return err
}
}
dst.Set(sliceVal)
return nil
case reflect.Struct:
fields := dst.NumField()
for i := range fields {
field := dst.Field(i)
tag := dst.Type().Field(i).Tag
skip := tag.Get("bare") == "-skip-"
if skip {
continue
}
if err := decodeValue(r, field); err != nil {
return err
}
}
return nil
case reflect.String:
n, err := DecodeUInt(r)
if err != nil {
return fmt.Errorf("expected a error: %w", err)
}
bytes := make([]byte, 0, n)
for range n {
b, err := r.ReadByte()
if err != nil {
return err
}
bytes = append(bytes, b)
}
dst.SetString(string(bytes))
return nil
default:
return fmt.Errorf("unsupported type for BARE: %s", dst.Type().Name())
}
}
func Decode(r io.ByteReader, dst interface{}) error {
ptrToVal := reflect.ValueOf(dst)
if ptrToVal.Type().Kind() != reflect.Pointer {
return errors.New("expected a pointer to something")
}
return decodeValue(r, ptrToVal.Elem())
}