|
1 | 1 | package bencode |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "errors" |
5 | 6 | "strconv" |
6 | 7 | ) |
7 | 8 |
|
8 | | -func readUntil(data []byte, symbol byte) ([]byte, int, bool) { |
9 | | - for i, b := range data { |
10 | | - if b == symbol { |
11 | | - return data[:i], i, true |
12 | | - } |
13 | | - } |
14 | | - return nil, 0, false |
| 9 | +func Unmarshal(data []byte) (interface{}, error) { |
| 10 | + return (&unmarshaler{ |
| 11 | + data: data, |
| 12 | + length: len(data), |
| 13 | + }).unmarshal() |
15 | 14 | } |
16 | 15 |
|
17 | | -func Unmarshal(data []byte) (interface{}, error) { |
18 | | - result, _, err := unmarshal(data) |
19 | | - if err != nil { |
20 | | - return nil, err |
21 | | - } |
22 | | - return result, nil |
| 16 | +type unmarshaler struct { |
| 17 | + data []byte |
| 18 | + length int |
| 19 | + cursor int |
23 | 20 | } |
24 | 21 |
|
25 | | -func unmarshal(data []byte) (interface{}, int, error) { |
26 | | - switch data[0] { |
| 22 | +func (u *unmarshaler) unmarshal() (interface{}, error) { |
| 23 | + switch u.data[u.cursor] { |
27 | 24 | case 'i': |
28 | | - integerBuffer, length, ok := readUntil(data[1:], 'e') |
29 | | - if !ok { |
30 | | - return nil, 0, errors.New("bencode: invalid integer field") |
| 25 | + u.cursor += 1 |
| 26 | + index := bytes.IndexByte(u.data[u.cursor:], 'e') |
| 27 | + if index == -1 { |
| 28 | + return nil, errors.New("bencode: invalid integer field") |
31 | 29 | } |
32 | | - integer, err := strconv.ParseInt(b2s(integerBuffer), 10, 64) |
| 30 | + index += u.cursor |
| 31 | + integer, err := strconv.ParseInt(b2s(u.data[u.cursor:index]), 10, 64) |
33 | 32 | if err != nil { |
34 | | - return nil, 0, err |
| 33 | + return nil, err |
35 | 34 | } |
36 | | - return integer, length + 2, nil |
| 35 | + u.cursor = index + 1 |
| 36 | + return integer, nil |
37 | 37 |
|
38 | 38 | case 'l': |
| 39 | + u.cursor += 1 |
39 | 40 | list := []interface{}{} |
40 | | - totalLength := 2 |
41 | | - data = data[1:] |
42 | 41 | for { |
43 | | - if len(data) == 0 { |
44 | | - return nil, 0, errors.New("bencode: invalid list field") |
| 42 | + if u.cursor == u.length { |
| 43 | + return nil, errors.New("bencode: invalid list field") |
45 | 44 | } |
46 | | - if data[0] == 'e' { |
47 | | - return list, totalLength, nil |
| 45 | + if u.data[u.cursor] == 'e' { |
| 46 | + u.cursor += 1 |
| 47 | + return list, nil |
48 | 48 | } |
49 | | - value, length, err := unmarshal(data) |
| 49 | + value, err := u.unmarshal() |
50 | 50 | if err != nil { |
51 | | - return nil, 0, err |
| 51 | + return nil, err |
52 | 52 | } |
53 | 53 | list = append(list, value) |
54 | | - data = data[length:] |
55 | | - totalLength += length |
56 | 54 | } |
57 | 55 |
|
58 | 56 | case 'd': |
| 57 | + u.cursor += 1 |
59 | 58 | dictionary := map[string]interface{}{} |
60 | | - totalLength := 2 |
61 | | - data = data[1:] |
62 | 59 | for { |
63 | | - if len(data) == 0 { |
64 | | - return nil, 0, errors.New("bencode: invalid dictionary field") |
| 60 | + if u.cursor == u.length { |
| 61 | + return nil, errors.New("bencode: invalid dictionary field") |
65 | 62 | } |
66 | | - if data[0] == 'e' { |
67 | | - return dictionary, totalLength, nil |
| 63 | + if u.data[u.cursor] == 'e' { |
| 64 | + u.cursor += 1 |
| 65 | + return dictionary, nil |
68 | 66 | } |
69 | | - value, length, err := unmarshal(data) |
| 67 | + value, err := u.unmarshal() |
70 | 68 | if err != nil { |
71 | | - return nil, 0, err |
| 69 | + return nil, err |
72 | 70 | } |
73 | 71 | key, ok := value.([]byte) |
74 | 72 | if !ok { |
75 | | - return nil, 0, errors.New("bencode: non-string dictionary key") |
| 73 | + return nil, errors.New("bencode: non-string dictionary key") |
76 | 74 | } |
77 | | - data = data[length:] |
78 | | - totalLength += length |
79 | | - value, length, err = unmarshal(data) |
| 75 | + value, err = u.unmarshal() |
80 | 76 | if err != nil { |
81 | | - return nil, 0, err |
| 77 | + return nil, err |
82 | 78 | } |
83 | 79 | dictionary[b2s(key)] = value |
84 | | - data = data[length:] |
85 | | - totalLength += length |
86 | 80 | } |
87 | 81 |
|
88 | 82 | default: |
89 | | - stringLengthBuffer, length, ok := readUntil(data, ':') |
90 | | - if !ok { |
91 | | - return nil, 0, errors.New("bencode: invalid string field") |
| 83 | + index := bytes.IndexByte(u.data[u.cursor:], ':') |
| 84 | + if index == -1 { |
| 85 | + return nil, errors.New("bencode: invalid string field") |
92 | 86 | } |
93 | | - stringLength, err := strconv.ParseInt(b2s(stringLengthBuffer), 10, 64) |
| 87 | + index += u.cursor |
| 88 | + stringLength, err := strconv.ParseInt(b2s(u.data[u.cursor:index]), 10, 64) |
94 | 89 | if err != nil { |
95 | | - return nil, 0, err |
| 90 | + return nil, err |
96 | 91 | } |
97 | | - endPosition := length + 1 + int(stringLength) |
98 | | - if endPosition > len(data) { |
99 | | - return nil, 0, errors.New("bencode: not a valid bencoded string") |
| 92 | + index += 1 |
| 93 | + endIndex := index + int(stringLength) |
| 94 | + if endIndex > u.length { |
| 95 | + return nil, errors.New("bencode: not a valid bencoded string") |
100 | 96 | } |
101 | | - return data[length+1 : endPosition], endPosition, nil |
| 97 | + value := u.data[index:endIndex] |
| 98 | + u.cursor = endIndex |
| 99 | + return value, nil |
102 | 100 | } |
103 | 101 | } |
0 commit comments