-
Notifications
You must be signed in to change notification settings - Fork 1
/
bencoder.js
214 lines (196 loc) · 5.25 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
'use strict'
const INTEGER_START = 'i'.charCodeAt(),
STRING_DELIM = ':'.charCodeAt(),
DICTIONARY_START = 'd'.charCodeAt(),
LIST_START = 'l'.charCodeAt(),
END_OF_TYPE = 'e'.charCodeAt();
var decoderProto = {
reset: function () {
this.position = 0;
this.input = null;
},
position: 0,
decode: function (buffer) {
let buffers = this.input? [this.input, buffer] : [buffer];
this.input = Buffer.concat(buffers);
if(!this.generator) {
this.generator = this.parse();
}
return this._decode();
},
_decode: function () {
let decodedValues = [];
while(true) {
let decoded = this.generator.next();
if(decoded.done) {
decodedValues.push(decoded.value);
this.input = this.input.slice(this.position);
this.position = 0;
this.generator = this.parse();
if(!this.input.length) break;
} else {
break;
}
}
return decodedValues.length ? decodedValues : false;
},
parse: function *(hint) { //hint about the type: for parseString
while(true) {
let type = hint || this.input[this.position];
switch(type) {
case DICTIONARY_START:
return yield *this.parseDictionary();
case LIST_START:
return yield *this.parseList();
case INTEGER_START:
return yield *this.parseInteger();
case undefined:
yield false;
break;
case END_OF_TYPE:
return null;
default:
if(type > 47 && type < 58) {
return yield *this.parseString();
}
this.throwError('Invalid bencode type', type, this.position);
}
}
},
parseString: function *() { // parse a string value as buffer
let value,
offset = this.position,
strLength = '',
length;
while(value === undefined) {
while(length === undefined) {
let chr;
while(this.position < this.input.length && (chr = this.input[this.position ++]) !== STRING_DELIM) {
if(chr < 48 || chr > 57) this.throwError('Invalid string length', chr, this.position - 1);
}
if(chr === STRING_DELIM) {
length = + this.input.slice(offset, this.position - 1).toString(); // no :
} else {
yield false;
}
}
while(this.input.length < this.position + length) {
yield false;
}
value = this.input.slice(this.position, this.position + length);
this.position += length;
return value.toString('binary');
}
},
parseInteger: function *() {
let intValue;
this.position ++;
let offset = this.position;
while(intValue === undefined) {
let chr;
while(this.position < this.input.length && (chr = this.input[this.position ++]) !== END_OF_TYPE) {
if(chr < 48 || chr > 57) this.throwError('Invalid integer value', chr, this.position - 1);
}
if(chr === END_OF_TYPE) {
intValue = + this.input.slice(offset, this.position - 1).toString();
} else {
yield false;
}
}
return intValue;
},
parseDictionary: function *() {
let dict = {};
this.position ++; //advance
while(this.input.length <= this.position) {
yield false;
}
while(this.input[this.position] !== END_OF_TYPE) {
//hint string
let key = yield *this.parse(50); //cannot use parseString here. At this point we're already within parseString and an END_OF_TYPE input will result in an error
if(key == null) break;
let value = yield *this.parse();
if(value !== null) {
dict[key.toString()] = value;
}
}
this.position ++;
return dict;
},
parseList: function *() {
let list = [];
this.position ++; //advance over l
while(this.input.length <= this.position) {
yield false;
}
while(this.input[this.position] !== END_OF_TYPE) {
let value = yield *this.parse();
if(value !== null) { //END_OF_TYPE?
list.push(value);
}
}
this.position ++;
return list;
},
throwError: function (error, char, position) {
throw new Error(error + " '" + String.fromCharCode(char) + "' at position " + position);
}
}
var encoder = {
encodeString: function (s) {
return s.length + ':' + s;
},
encodeBuffer: function (b) {
return this.encodeString(b.toString('binary'));
},
encodeInteger: function (i) {
return 'i' + i + 'e';
},
encodeDictionary: function (o) {
var value = 'd';
for(var i in o) {
value += this.encodeString(i) + this.encode(o[i]);
}
value += 'e';
return value;
},
encodeList: function (array) {
var self = this;
var encoded = array.reduce(function (value, el) {
return value + self.encode(el);
}, '');
return 'l' + encoded + 'e';
},
encode: function (value) {
var type = typeof value;
switch(type) {
case 'string':
return this.encodeString(value);
break;
case 'number':
return this.encodeInteger(value);
break;
case 'undefined':
return 'undefined';
break;
default:
if(value === null) {
return 'null';
} else if(Array.isArray(value)) {
return this.encodeList(value);
} else if(Buffer.isBuffer(value)) {
return this.encodeBuffer(value);
} else if(type == 'object') {
return this.encodeDictionary(value);
}
}
}
};
module.exports = {
decoder: function () {
var decoder = Object.create(decoderProto);
decoder.position = 0;
return decoder;
},
encoder: encoder //state-less
};