-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
164 lines (133 loc) · 3.08 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
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
package bencode
import (
"bytes"
"errors"
"strconv"
)
var ErrBadEncoding = errors.New("bad encoding")
var ErrInvalidInput = errors.New("invalid input")
var StringDelimiter = []byte(":")
const (
DigitLow = byte('0')
DigitHigh = byte('9')
IntegerStart = byte('i')
IntegerEnd = byte('e')
ListStart = byte('l')
ListEnd = byte('e')
DictionaryStart = byte('d')
DictionaryEnd = byte('e')
)
// Decode will return either string, int64, []interface{} or map[string]interface{},
// interface{} being anything of these 4 types
func Decode(data []byte) (interface{}, error) {
obj, _, err := decodeNextObject(data)
return obj, err
}
// decodeNextObject returns value of the next object, offset of the value after it and error
func decodeNextObject(data []byte) (interface{}, int, error) {
if len(data) == 0 {
return nil, 0, ErrInvalidInput
}
c := data[0]
switch {
case c >= DigitLow && c <= DigitHigh:
return decodeString(data)
case c == IntegerStart:
return decodeInteger(data)
case c == ListStart:
return decodeList(data)
case c == DictionaryStart:
return decodeDictionary(data)
}
return nil, 0, ErrBadEncoding
}
func decodeString(data []byte) (string, int, error) {
c := data[0]
if c < DigitLow || c > DigitHigh {
return "", 0, ErrBadEncoding
}
parts := bytes.SplitN(data, StringDelimiter, 2)
switch {
case len(parts) != 2 && c != DigitLow:
return "", 0, ErrBadEncoding
case c == DigitLow:
return "", 2, nil
}
// TODO: maybe strconv.ParseInt(string(parts[0]), 10, 64) if we want int64
l, err := strconv.Atoi(string(parts[0]))
if err != nil {
return "", 0, err
}
if len(parts[1]) < l {
return "", 0, ErrBadEncoding
}
return string(parts[1][:l]), len(parts[0]) + 1 + l, nil
}
func decodeInteger(data []byte) (int64, int, error) {
c := data[0]
if c != IntegerStart {
return 0, 0, ErrBadEncoding
}
e := bytes.IndexByte(data, IntegerEnd)
if e < 2 {
return 0, 0, ErrBadEncoding
}
n, err := strconv.ParseInt(string(data[1:e]), 10, 64)
if err != nil {
return 0, 0, err
}
return n, e + 1, err
}
func decodeList(data []byte) ([]interface{}, int, error) {
c := data[0]
if c != ListStart {
return nil, 0, ErrBadEncoding
}
var list []interface{}
p := 1
for {
if data[p] == ListEnd {
break
}
obj, d, err := decodeNextObject(data[p:])
if err != nil {
return nil, 0, err
}
list = append(list, obj)
p += d
}
return list, p + 1, nil
}
func decodeDictionary(data []byte) (map[string]interface{}, int, error) {
c := data[0]
if c != DictionaryStart {
return nil, 0, ErrBadEncoding
}
dict := make(map[string]interface{})
lastKey := ""
p := 1 // offset in the input slice
for {
if data[p] == DictionaryEnd {
break
}
// key must be a string
key, i, err := decodeString(data[p:])
if err != nil {
return nil, 0, err
}
// keys must be ordered
if p > 1 && lastKey >= key {
return nil, 0, ErrBadEncoding
}
lastKey = key
p += i
// value
val, j, err := decodeNextObject(data[p:])
if err != nil {
return nil, 0, err
}
p += j
dict[key] = val
}
return dict, p + 1, nil
}