-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
146 lines (135 loc) · 3.19 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
package bencode
import (
"bufio"
"fmt"
"io"
"strings"
)
// Just to keep track of read bytes for error messages
type countReader struct {
*bufio.Reader
count int
}
func (r *countReader) ReadByte() (byte, error) {
r.count++
return r.Reader.ReadByte()
}
func decode(r io.Reader) (any, error) {
br := &countReader{
Reader: bufio.NewReader(r),
count: 0,
}
k, err := br.ReadByte()
if err != nil {
return nil, err
}
return decodeItem(k, br)
}
func decodeItem(key byte, r *countReader) (any, error) {
switch key {
case 'd':
return decodeDict(r)
case 'l':
return decodeList(r)
case 'i':
return decodeInt(r)
default:
if key < '0' || key > '9' {
return nil, fmt.Errorf("invalid char %s index %d", string(key), r.count)
}
return decodeString(key, r)
}
}
func decodeInt(r *countReader) (int, error) {
integer := 0
for {
b, err := r.ReadByte()
if err != nil {
return 0, fmt.Errorf("failed reading int at index %d -> %w", r.count, err)
}
if b == 'e' {
break
}
if b < '0' || b > '9' {
return 0, fmt.Errorf("invalid char %s reading int at index %d", string(b), r.count)
}
integer = integer*10 + int(b-'0')
}
return integer, nil
}
func decodeString(previous byte, r *countReader) (string, error) {
length := int(previous - '0')
for {
b, err := r.ReadByte()
if err != nil {
return "", fmt.Errorf("failed decoding string length at index %d. r.ReadByte() -> %w", r.count, err)
}
if b == ':' {
break
}
if b < '0' || b > '9' {
return "", fmt.Errorf("invalid char %s decoding string length at index %d", string(b), r.count)
}
length = length*10 + int(b-'0')
}
read := 0
var sb strings.Builder
for {
if length == read {
break
}
b, err := r.ReadByte()
if err != nil {
return "", fmt.Errorf("failed decoding string at index %d. r.ReadByte() -> %w", r.count, err)
}
if err := sb.WriteByte(b); err != nil {
return "", fmt.Errorf("failed decoding string at index %d. sb.WriteByte(%s): %w", r.count, string(b), err)
}
read++
}
return sb.String(), nil
}
func decodeList(r *countReader) ([]any, error) {
list := make([]any, 0)
for {
b, err := r.ReadByte()
if err != nil {
return nil, fmt.Errorf("failed decoding list at index %d. r.ReadByte() -> %w", r.count, err)
}
if b == 'e' {
break
}
item, err := decodeItem(b, r)
if err != nil {
return nil, fmt.Errorf("failed decoding list at index %d. decodeItem() -> %w", r.count, err)
}
list = append(list, item)
}
return list, nil
}
func decodeDict(r *countReader) (map[string]any, error) {
dict := make(map[string]any, 0)
for {
b, err := r.ReadByte()
if err != nil {
return nil, fmt.Errorf("failed decoding dict at index %d. r.ReadByte() -> %w", r.count, err)
}
if b == 'e' {
break
}
key, err := decodeString(b, r)
if err != nil {
return nil, fmt.Errorf("failed decoding dict at index %d. decodeString() -> %w", r.count, err)
}
b, err = r.ReadByte()
if err != nil {
return nil, fmt.Errorf("failed decoding dict at index %d. r.ReadByte() -> %w", r.count, err)
}
val, err := decodeItem(b, r)
if err != nil {
return nil, fmt.Errorf("failed decoding dict at index %d. decodeItem() -> %w", r.count, err)
}
dict[key] = val
}
return dict, nil
}