-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode.go
187 lines (165 loc) · 3.76 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package bencode
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"strconv"
)
// A Decoder reads and decodes Bencode values from an input stream.
type Decoder struct {
r io.Reader
fromReader bool
data []byte
length int
cursor int
}
// NewDecoder returns a new decoder that reads from r.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{r: r, fromReader: true}
}
// NewDecodeBytes returns a new decoder that decodes given bytes.
func NewDecodeBytes(src []byte) *Decoder {
d := &Decoder{
data: src,
length: len(src),
}
return d
}
// Decode writes the Bencode encoding of v to the stream.
func (d *Decoder) Decode(v any) error {
switch v.(type) {
case nil:
return errors.New("bencode: cannot marshal into nil")
default:
// we cannot catch other types, cause this might be a struct
// so decoding below even if `v` isn't a supported type.
}
if d.fromReader {
var err error
d.data, err = ioutil.ReadAll(d.r)
if err != nil {
return fmt.Errorf("bencode: cannot read from reader: %w", err)
}
d.length = len(d.data)
}
if d.length == 0 {
return errors.New("bencode: cannot decode empty input")
}
got, err := d.unmarshal()
if err != nil {
return fmt.Errorf("bencode: decode failed: %w", err)
}
return d.writeResult(v, got)
}
func (d *Decoder) writeResult(v, got any) error {
switch v := v.(type) {
case *any: // catch any type
*v = got
case *map[string]any:
val, ok := got.(map[string]any)
if !ok {
return fmt.Errorf("bencode: cannot decode %T into map", got)
}
*v = val
case *[]any:
val, ok := got.([]any)
if !ok {
return fmt.Errorf("bencode: cannot decode %T into list", got)
}
*v = val
default:
// TODO: decode into struct if got map
}
return nil
}
func (d *Decoder) unmarshal() (any, error) {
switch d.data[d.cursor] {
case 'i':
return d.unmarshalInt()
case 'd':
return d.unmarshalMap()
case 'l':
return d.unmarshalList()
default:
return d.unmarshalString()
}
}
func (d *Decoder) unmarshalInt() (int64, error) {
d.cursor++
index := bytes.IndexByte(d.data[d.cursor:], 'e')
if index == -1 {
return 0, errors.New("cannot process invalid integer")
}
index += d.cursor
integer, err := strconv.ParseInt(b2s(d.data[d.cursor:index]), 10, 64)
if err != nil {
return 0, err
}
d.cursor = index + 1
return integer, nil
}
func (d *Decoder) unmarshalMap() (any, error) {
dictionary := make(map[string]any)
d.cursor++
for {
if d.cursor == d.length {
return nil, errors.New("cannot process invalid dictionary")
}
if d.data[d.cursor] == 'e' {
d.cursor++
return dictionary, nil
}
// try to decode a dict key. it's a string by the Bencode specification
key, err := d.unmarshalString()
if err != nil {
return nil, err
}
value, err := d.unmarshal()
if err != nil {
return nil, err
}
dictionary[b2s(key)] = value
}
}
func (d *Decoder) unmarshalList() (any, error) {
list := make([]any, 0)
d.cursor++
for {
if d.cursor == d.length {
return nil, errors.New("cannot process invalid list")
}
if d.data[d.cursor] == 'e' {
d.cursor++
return list, nil
}
value, err := d.unmarshal()
if err != nil {
return nil, err
}
list = append(list, value)
}
}
func (d *Decoder) unmarshalString() ([]byte, error) {
index := bytes.IndexByte(d.data[d.cursor:], ':')
if index == -1 {
return nil, errors.New("cannot process invalid string")
}
index += d.cursor
strLen, err := strconv.ParseInt(b2s(d.data[d.cursor:index]), 10, 64)
if err != nil {
return nil, err
}
if strLen < 0 {
return nil, errors.New("string length can not be a negative")
}
index++
endIndex := index + int(strLen)
if endIndex > d.length {
return nil, errors.New("string length is not correct")
}
value := d.data[index:endIndex]
d.cursor = endIndex
return value, nil
}