forked from Onek8/LNXSDK
183 lines
4.6 KiB
Haxe
183 lines
4.6 KiB
Haxe
package leenkx.network.torrent;
|
|
|
|
import haxe.io.Bytes;
|
|
|
|
class RawDict {
|
|
public var pairs:Array<{key:Bytes, val:Dynamic}> = [];
|
|
public function new() {}
|
|
}
|
|
|
|
class Bencode {
|
|
|
|
public static function encode(value:Dynamic):Bytes {
|
|
var out = new haxe.io.BytesOutput();
|
|
encodeValue(out, value);
|
|
return out.getBytes();
|
|
}
|
|
|
|
public static function encodeToString(value:Dynamic):String {
|
|
return encode(value).toString();
|
|
}
|
|
|
|
static function encodeValue(out:haxe.io.BytesOutput, value:Dynamic):Void {
|
|
if (Std.isOfType(value, Bool)) {
|
|
encodeInt(out, value ? 1 : 0);
|
|
} else if (Std.isOfType(value, Int) || Std.isOfType(value, Float)) {
|
|
encodeInt(out, value);
|
|
} else if (Std.isOfType(value, String)) {
|
|
encodeString(out, value);
|
|
} else if (Std.isOfType(value, Bytes)) {
|
|
encodeBytes(out, value);
|
|
} else if (Std.isOfType(value, RawDict)) {
|
|
encodeRawDict(out, value);
|
|
} else if (Std.isOfType(value, Array)) {
|
|
encodeList(out, value);
|
|
} else if (value != null && Reflect.isObject(value)) {
|
|
encodeDict(out, value);
|
|
} else {
|
|
throw "Bencode: cannot encode " + Type.typeof(value);
|
|
}
|
|
}
|
|
|
|
static function encodeInt(out:haxe.io.BytesOutput, value:Float):Void {
|
|
out.writeString("i");
|
|
var s = Std.string(value);
|
|
var dot = s.indexOf(".");
|
|
if (dot != -1) s = s.substring(0, dot);
|
|
if (s.indexOf("e") != -1 || s.indexOf("E") != -1) {
|
|
s = Std.string(Math.floor(value));
|
|
dot = s.indexOf(".");
|
|
if (dot != -1) s = s.substring(0, dot);
|
|
}
|
|
out.writeString(s);
|
|
out.writeString("e");
|
|
}
|
|
|
|
static function encodeString(out:haxe.io.BytesOutput, value:String):Void {
|
|
var bytes = Bytes.ofString(value);
|
|
encodeBytes(out, bytes);
|
|
}
|
|
|
|
static function encodeBytes(out:haxe.io.BytesOutput, bytes:Bytes):Void {
|
|
out.writeString(Std.string(bytes.length));
|
|
out.writeString(":");
|
|
out.writeBytes(bytes, 0, bytes.length);
|
|
}
|
|
|
|
static function encodeRawDict(out:haxe.io.BytesOutput, dict:RawDict):Void {
|
|
out.writeString("d");
|
|
for (p in dict.pairs) {
|
|
encodeBytes(out, p.key);
|
|
encodeValue(out, p.val);
|
|
}
|
|
out.writeString("e");
|
|
}
|
|
|
|
static function encodeList(out:haxe.io.BytesOutput, arr:Array<Dynamic>):Void {
|
|
out.writeString("l");
|
|
for (item in arr) {
|
|
encodeValue(out, item);
|
|
}
|
|
out.writeString("e");
|
|
}
|
|
|
|
static function encodeDict(out:haxe.io.BytesOutput, obj:Dynamic):Void {
|
|
out.writeString("d");
|
|
var keys = Reflect.fields(obj);
|
|
keys.sort(Reflect.compare);
|
|
for (key in keys) {
|
|
if (Reflect.field(obj, key) == null) continue;
|
|
encodeString(out, key);
|
|
encodeValue(out, Reflect.field(obj, key));
|
|
}
|
|
out.writeString("e");
|
|
}
|
|
|
|
public static function decode(bytes:Bytes):Dynamic {
|
|
return new Decoder(bytes).value();
|
|
}
|
|
|
|
public static function decodeString(str:String):Dynamic {
|
|
return decode(Bytes.ofString(str));
|
|
}
|
|
}
|
|
|
|
private class Decoder {
|
|
static inline var MAX_DECODE_LEN = 10 * 1024 * 1024; // 10 MB
|
|
|
|
var data:Bytes;
|
|
var pos:Int = 0;
|
|
|
|
public function new(data:Bytes) {
|
|
this.data = data;
|
|
}
|
|
|
|
public function value():Dynamic {
|
|
if (pos >= data.length) throw "Bencode: truncated input";
|
|
var b = data.get(pos++);
|
|
switch (b) {
|
|
case 'i'.code:
|
|
return decodeInt();
|
|
case 'l'.code:
|
|
return decodeList();
|
|
case 'd'.code:
|
|
return decodeDict();
|
|
default:
|
|
return decodeBytes();
|
|
}
|
|
}
|
|
|
|
function decodeInt():Float {
|
|
var start = pos;
|
|
while (pos < data.length && data.get(pos) != 'e'.code) pos++;
|
|
if (pos >= data.length) throw "Bencode: unterminated int";
|
|
var s = data.getString(start, pos - start);
|
|
pos++;
|
|
var v = Std.parseFloat(s);
|
|
if (Math.isNaN(v)) throw "Bencode: bad int " + s;
|
|
return v;
|
|
}
|
|
|
|
function decodeBytes():Bytes {
|
|
var start = pos - 1;
|
|
if (data.get(start) < '0'.code || data.get(start) > '9'.code) {
|
|
throw "Bencode: bad byte length";
|
|
}
|
|
while (pos < data.length && data.get(pos) != ':'.code) pos++;
|
|
if (pos >= data.length) throw "Bencode: unterminated length";
|
|
var len = Std.parseInt(data.getString(start, pos - start));
|
|
pos++;
|
|
if (len == null || len < 0 || len > MAX_DECODE_LEN
|
|
|| pos + len > data.length) {
|
|
throw "Bencode: invalid byte length " + len;
|
|
}
|
|
var bytes = Bytes.alloc(len);
|
|
bytes.blit(0, data, pos, len);
|
|
pos += len;
|
|
return bytes;
|
|
}
|
|
|
|
function decodeList():Array<Dynamic> {
|
|
var arr:Array<Dynamic> = [];
|
|
while (true) {
|
|
if (pos >= data.length) throw "Bencode: unterminated list";
|
|
if (data.get(pos) == 'e'.code) break;
|
|
arr.push(value());
|
|
}
|
|
pos++;
|
|
return arr;
|
|
}
|
|
|
|
function decodeDict():Dynamic {
|
|
var obj:Dynamic = {};
|
|
while (true) {
|
|
if (pos >= data.length) throw "Bencode: unterminated dict";
|
|
if (data.get(pos) == 'e'.code) break;
|
|
var key:Bytes = value();
|
|
Reflect.setField(obj, key.toString(), value());
|
|
}
|
|
pos++;
|
|
return obj;
|
|
}
|
|
}
|