-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbencoder.js
195 lines (195 loc) · 5.62 KB
/
bencoder.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
'use strict';
var buffer_1 = require('buffer');
/*
Encoding
*/
function encodeString(s) {
var bytes = buffer_1.Buffer.from(s);
return buffer_1.Buffer.concat([
buffer_1.Buffer.from(bytes.length.toString()),
buffer_1.Buffer.from(':'),
bytes]);
}
function encodeInteger(i) {
return buffer_1.Buffer.from("i" + i + "e");
}
function encodeArray(l) {
var result = [buffer_1.Buffer.from('l')];
l.forEach(function (element) {
result.push(_encode(element));
});
result.push(buffer_1.Buffer.from('e'));
return buffer_1.Buffer.concat(result);
}
function encodeDict(d) {
var result = [buffer_1.Buffer.from('d')];
var keys = Object.keys(d).sort();
keys.forEach(function (k) {
result.push(encodeString(k));
result.push(encode(d[k]));
});
result.push(buffer_1.Buffer.from('e'));
return buffer_1.Buffer.concat(result);
}
function _encode(data) {
switch (typeof data) {
case 'string':
return encodeString(data);
case 'number':
return encodeInteger(data);
case 'object':
if (Array.isArray(data)) {
return encodeArray(data);
}
else {
return encodeDict(data);
}
}
}
/**
* Encodes data in bencode.
*
* @param {Array|String|Object|Number} data
* @return {Buffer}
*/
function encode(data) {
return new buffer_1.Buffer(_encode(data));
}
exports.encode = encode;
/*
Decoding
*/
var Delimeters;
(function (Delimeters) {
Delimeters[Delimeters["i"] = 105] = "i";
Delimeters[Delimeters["e"] = 101] = "e";
Delimeters[Delimeters["l"] = 108] = "l";
Delimeters[Delimeters["d"] = 100] = "d";
Delimeters[Delimeters["colon"] = 58] = "colon";
})(Delimeters || (Delimeters = {}));
function decodeString(data, encoding) {
var firstColonIndex = null;
for (var i = 0; i < data.length; i++) {
if (data[i] === Delimeters.colon) {
firstColonIndex = i;
break;
}
}
if (!firstColonIndex) {
throw new Error("Error while decoding " + data.toString(encoding) + ". Missing colon.");
}
var expectedLength = parseInt(data.slice(0, firstColonIndex).toString());
var value = data.slice(firstColonIndex + 1, firstColonIndex + 1 + expectedLength);
return { value: value.toString(encoding), rest: data.slice(firstColonIndex + 1 + expectedLength) };
}
function decodeInteger(data) {
var endIndex = null; // The first 'e' symbol index
for (var i = 1; i < data.length; i++) {
if (data[i] === Delimeters.e) {
endIndex = i;
break;
}
}
if (!endIndex) {
throw new Error("Invalid data. 'e' symbol expected");
}
return {
value: parseInt(data.slice(1, endIndex).toString()),
rest: data.slice(endIndex + 1)
};
}
function decodeList(data, encoding) {
// TODO: Check for 'e' symbol in the end.
var result = [];
var rest = data.slice(1); // l...
var value = null;
while (true) {
var firstByte = rest[0];
if (firstByte === Delimeters.i) {
(_a = decodeInteger(rest), value = _a.value, rest = _a.rest, _a);
result.push(value);
continue;
}
if (encodesDigit(firstByte)) {
(_b = decodeString(rest, encoding), value = _b.value, rest = _b.rest, _b);
result.push(value);
continue;
}
if (firstByte === Delimeters.l) {
(_c = decodeList(rest, encoding), value = _c.value, rest = _c.rest, _c);
result.push(value);
continue;
}
if (firstByte === Delimeters.d) {
(_d = decodeDict(rest, encoding), value = _d.value, rest = _d.rest, _d);
result.push(value);
continue;
}
if (firstByte === Delimeters.e) {
rest = rest.slice(1);
break;
}
throw new Error("Expected d, i, l or digit, got " + rest.toString(encoding));
}
return { value: result, rest: rest };
var _a, _b, _c, _d;
}
function decodeDict(data, encoding) {
var result = {};
var rest = data.slice(1); // d...
var value = null;
var key;
while (rest.length !== 0) {
var firstByte = rest[0];
if (firstByte === Delimeters.e) {
rest = rest.slice(1);
break;
}
(_a = decodeString(rest, encoding), key = _a.value, rest = _a.rest, _a);
(_b = _decode(rest, encoding), value = _b.value, rest = _b.rest, _b);
result[key] = value;
}
return { value: result, rest: rest };
var _a, _b;
}
function encodesDigit(x) {
return (x >= 0x30) && (x <= 0x39);
}
function _decode(data, encoding) {
var value = null;
var rest = null;
var firstByte = data[0];
if (firstByte === Delimeters.i) {
return decodeInteger(data);
}
else if (encodesDigit(firstByte)) {
return decodeString(data, encoding);
}
else if (firstByte === Delimeters.l) {
return decodeList(data, encoding);
}
else if (firstByte === Delimeters.d) {
return decodeDict(data, encoding);
}
else {
throw new Error("Expected d, i, l or digit, got \"" + data.toString() + "\"");
}
}
/**
* Decodes bencoded data.
*
* @param {Buffer} data
* @param {String} encoding (optional)
* @return {Object|Array|String|Number}
*/
function decode(data, encoding) {
var value;
var rest;
(_a = _decode(data, encoding), value = _a.value, rest = _a.rest, _a);
if (rest.length) {
throw new Error("Unexpected continuation: \"" + rest.toString() + "\"");
}
return value;
var _a;
}
exports.decode = decode;