/
decode.js
173 lines (135 loc) · 3.45 KB
/
decode.js
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
import { arr2text, text2arr, arr2hex } from 'uint8-util'
const INTEGER_START = 0x69 // 'i'
const STRING_DELIM = 0x3A // ':'
const DICTIONARY_START = 0x64 // 'd'
const LIST_START = 0x6C // 'l'
const END_OF_TYPE = 0x65 // 'e'
/**
* replaces parseInt(buffer.toString('ascii', start, end)).
* For strings with less then ~30 charachters, this is actually a lot faster.
*
* @param {Uint8Array} data
* @param {Number} start
* @param {Number} end
* @return {Number} calculated number
*/
function getIntFromBuffer (buffer, start, end) {
let sum = 0
let sign = 1
for (let i = start; i < end; i++) {
const num = buffer[i]
if (num < 58 && num >= 48) {
sum = sum * 10 + (num - 48)
continue
}
if (i === start && num === 43) { // +
continue
}
if (i === start && num === 45) { // -
sign = -1
continue
}
if (num === 46) { // .
// its a float. break here.
break
}
throw new Error('not a number: buffer[' + i + '] = ' + num)
}
return sum * sign
}
/**
* Decodes bencoded data.
*
* @param {Uint8Array} data
* @param {Number} start (optional)
* @param {Number} end (optional)
* @param {String} encoding (optional)
* @return {Object|Array|Uint8Array|String|Number}
*/
function decode (data, start, end, encoding) {
if (data == null || data.length === 0) {
return null
}
if (typeof start !== 'number' && encoding == null) {
encoding = start
start = undefined
}
if (typeof end !== 'number' && encoding == null) {
encoding = end
end = undefined
}
decode.position = 0
decode.encoding = encoding || null
decode.data = !(ArrayBuffer.isView(data))
? text2arr(data)
: new Uint8Array(data.slice(start, end))
decode.bytes = decode.data.length
return decode.next()
}
decode.bytes = 0
decode.position = 0
decode.data = null
decode.encoding = null
decode.next = function () {
switch (decode.data[decode.position]) {
case DICTIONARY_START:
return decode.dictionary()
case LIST_START:
return decode.list()
case INTEGER_START:
return decode.integer()
default:
return decode.buffer()
}
}
decode.find = function (chr) {
let i = decode.position
const c = decode.data.length
const d = decode.data
while (i < c) {
if (d[i] === chr) return i
i++
}
throw new Error(
'Invalid data: Missing delimiter "' +
String.fromCharCode(chr) + '" [0x' +
chr.toString(16) + ']'
)
}
decode.dictionary = function () {
decode.position++
const dict = {}
while (decode.data[decode.position] !== END_OF_TYPE) {
const buffer = decode.buffer()
let key = arr2text(buffer)
if (key.includes('\uFFFD')) key = arr2hex(buffer)
dict[key] = decode.next()
}
decode.position++
return dict
}
decode.list = function () {
decode.position++
const lst = []
while (decode.data[decode.position] !== END_OF_TYPE) {
lst.push(decode.next())
}
decode.position++
return lst
}
decode.integer = function () {
const end = decode.find(END_OF_TYPE)
const number = getIntFromBuffer(decode.data, decode.position + 1, end)
decode.position += end + 1 - decode.position
return number
}
decode.buffer = function () {
let sep = decode.find(STRING_DELIM)
const length = getIntFromBuffer(decode.data, decode.position, sep)
const end = ++sep + length
decode.position = end
return decode.encoding
? arr2text(decode.data.slice(sep, end))
: decode.data.slice(sep, end)
}
export default decode