forked from jackpal/bencode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incswparse.go
131 lines (109 loc) · 3.36 KB
/
incswparse.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
package bencode
import (
"bufio"
"bytes"
"errors"
"strconv"
)
// A relatively fast unmarshaler.
// Adapted from https://github.com/IncSW/go-bencode/blob/master/unmarshaler.go
// License: https://github.com/IncSW/go-bencode/blob/master/LICENSE
// Differences from IncSW are for compatibility with the existing bencode-go API:
// (a) Uses a bufio.Reader rather than a raw []byte
// (b) Strings are returned as golang strings rather than as raw []byte arrays.
func decodeFromReader(r *bufio.Reader) (data interface{}, err error) {
result, err := unmarshal(r)
if err != nil {
return nil, err
}
return result, nil
}
func unmarshal(data *bufio.Reader) (interface{}, error) {
ch, err := data.ReadByte()
if err != nil {
return nil, err
}
switch ch {
case 'i':
integerBuffer, err := optimisticReadBytes(data, 'e')
if err != nil {
return nil, err
}
integerBuffer = integerBuffer[:len(integerBuffer)-1]
integer, err := strconv.ParseInt(string(integerBuffer), 10, 64)
if err != nil {
return nil, err
}
return integer, nil
case 'l':
list := []interface{}{}
for {
c, err2 := data.ReadByte()
if err2 == nil {
if c == 'e' {
return list, nil
} else {
data.UnreadByte()
}
}
value, err := unmarshal(data)
if err != nil {
return nil, err
}
list = append(list, value)
}
case 'd':
dictionary := map[string]interface{}{}
for {
c, err2 := data.ReadByte()
if err2 == nil {
if c == 'e' {
return dictionary, nil
} else {
data.UnreadByte()
}
}
value, err := unmarshal(data)
if err != nil {
return nil, err
}
key, ok := value.(string)
if !ok {
return nil, errors.New("bencode: non-string dictionary key")
}
value, err = unmarshal(data)
if err != nil {
return nil, err
}
dictionary[key] = value
}
default:
data.UnreadByte()
stringLengthBuffer, err := optimisticReadBytes(data, ':')
if err != nil {
return nil, err
}
stringLengthBuffer = stringLengthBuffer[:len(stringLengthBuffer)-1]
stringLength, err := strconv.ParseInt(string(stringLengthBuffer), 10, 64)
if err != nil {
return nil, err
}
buf := make([]byte, stringLength)
_, err = readAtLeast(data, buf, int(stringLength))
return string(buf), err
}
}
// Reads bytes out of local buffer if possible, which avoids an extra copy.
// The result []byte is only guarenteed to be valid until the next call to a Read method.
func optimisticReadBytes(data *bufio.Reader, delim byte) ([]byte, error) {
buffered := data.Buffered()
var buffer []byte
var err error
if buffer, err = data.Peek(buffered); err != nil {
return nil, err
}
if i := bytes.IndexByte(buffer, delim); i >= 0 {
return data.ReadSlice(delim)
}
return data.ReadBytes(delim)
}