-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
196 lines (182 loc) · 3.62 KB
/
unmarshal.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
package bencoding
import (
"errors"
"fmt"
"io"
"strconv"
)
type ByteReader interface {
ReadByte() (byte, error)
UnreadByte() error
Read([]byte) (int, error)
}
func Unmarshal(raw ByteReader) (result any, err error) {
b, err := raw.ReadByte()
if err != nil {
return nil, err
}
if err := raw.UnreadByte(); err != nil {
return nil, err
}
defer func() {
if err != nil {
result = nil
}
}()
switch b {
case 'i':
return UnmarshalInt(raw)
case 'l':
return UnmarshalList(raw)
case 'd':
return UnmarshalDict(raw)
case '0':
fallthrough
case '1':
fallthrough
case '2':
fallthrough
case '3':
fallthrough
case '4':
fallthrough
case '5':
fallthrough
case '6':
fallthrough
case '7':
fallthrough
case '8':
fallthrough
case '9':
return UnmarshalString(raw)
default:
return nil, errors.New("unrecognized prefix")
}
}
func UnmarshalInt(raw ByteReader) (int, error) {
b, err := raw.ReadByte()
if err != nil {
return 0, err
}
if b != 'i' {
return 0, errors.New("int: encoding must begin with 'i'")
}
intBytes := make([]byte, 0)
b, err = raw.ReadByte()
if err != nil {
return 0, err
}
if (b < '0' || b > '9') && b != '-' {
return 0, errors.New("int encoding must start with a number or '-'")
}
if b == '0' {
b, err = raw.ReadByte()
if err != nil {
return 0, err
}
if b != 'e' {
return 0, errors.New("int: encoding cannot have leading zeros")
}
return 0, nil
}
intBytes = append(intBytes, b)
for b, err = raw.ReadByte(); err == nil; b, err = raw.ReadByte() {
if b < '0' || b > '9' {
if b != 'e' {
return 0, errors.New("int: encoding must end with 'e'")
}
break
}
intBytes = append(intBytes, b)
}
if err != nil {
return 0, err
}
return strconv.Atoi(string(intBytes))
}
func UnmarshalList(raw ByteReader) ([]any, error) {
b, err := raw.ReadByte()
if err != nil {
return nil, err
}
if b != 'l' {
return nil, errors.New("list: encoding must begin with 'l'")
}
var l []any
for {
b, err = raw.ReadByte()
if err != nil {
return nil, err
}
if b == 'e' {
return l, nil
}
if err = raw.UnreadByte(); err != nil {
return nil, err
}
elem, err := Unmarshal(raw)
if err != nil {
return nil, err
}
l = append(l, elem)
}
}
func UnmarshalDict(raw ByteReader) (map[string]any, error) {
b, err := raw.ReadByte()
if err != nil {
return nil, err
}
if b != 'd' {
return nil, errors.New("dict: encoding must begin with 'd'")
}
dict := make(map[string]any)
for {
b, err = raw.ReadByte()
if err != nil {
return nil, err
}
if b == 'e' {
return dict, nil
}
if err := raw.UnreadByte(); err != nil {
return nil, fmt.Errorf("dict: unable to parse key: %w", err)
}
key, err := UnmarshalString(raw)
if err != nil {
return nil, err
}
value, err := Unmarshal(raw)
if err != nil {
return nil, fmt.Errorf("dict: unable to parse value for %q: %w", key, err)
}
dict[key] = value
}
}
func UnmarshalString(raw ByteReader) (string, error) {
lenBytes := make([]byte, 0)
var b byte
var err error
for b, err = raw.ReadByte(); err == nil; b, err = raw.ReadByte() {
if b == ':' {
break
}
if b < '0' || b > '9' {
return "", errors.New("string: encoding must start with length")
}
lenBytes = append(lenBytes, b)
}
if err != nil {
return "", err
}
strLen, err := strconv.Atoi(string(lenBytes))
if err != nil {
return "", err
}
strBytes := make([]byte, strLen, strLen)
bytesRead, err := io.ReadFull(raw, strBytes)
if err != nil {
return "", fmt.Errorf("string: unable to read declared string length (wanted %d bytes, read %d bytes): %w", strLen, bytesRead, err)
}
return string(strBytes), err
}