private static List <string> AllKeys(List <string> output, BencodedValue value) { AllKeys(output, value.Dictionary); AllKeys(output, value.Array); return(output); }
public byte[] Encode(BencodedValue data) { using (MemoryStream stream = new MemoryStream()) { this.Encode(stream, data); return(stream.ToArray()); } }
private static List <string> AllTexts(List <string> output, BencodedValue value, Encoding encoding) { if (value != null) { AllTexts(output, value.Text, encoding); AllTexts(output, value.Dictionary, encoding); AllTexts(output, value.Array, encoding); } return(output); }
public static T Find <T>(this BencodedValue value, string name, Func <BencodedValue, T> selector) { if (value != null && value.Dictionary != null) { foreach (BencodedEntry entry in value.Dictionary) { if (entry.Key.Text != null && entry.Key.Text.GetString() == name) { return(selector.Invoke(entry.Value)); } } } return(selector.Invoke(null)); }
private void Encode(MemoryStream stream, BencodedValue data) { if (data.Dictionary != null) { this.Write(stream, "d"); foreach (BencodedEntry entry in data.Dictionary) { this.Encode(stream, entry.Key); this.Encode(stream, entry.Value); } this.Write(stream, "e"); } else if (data.Array != null) { this.Write(stream, "l"); foreach (BencodedValue item in data.Array) { this.Encode(stream, item); } this.Write(stream, "e"); } else if (data.Text != null) { this.Write(stream, data.Text.Length.ToString()); this.Write(stream, ":"); this.Write(stream, data.Text.GetBytes()); } else if (data.Number != null) { this.Write(stream, "i"); this.Write(stream, data.Number.ToString()); this.Write(stream, "e"); } else { this.Write(stream, data.Data.Length.ToString()); this.Write(stream, ":"); this.Write(stream, data.Data.GetBytes()); } }
private BencodedValue DecodeDictionary(byte[] data, ref int position) { int offset = position - 1; List <BencodedEntry> entries = new List <BencodedEntry>(); while (data[position] != 0x65) { BencodedValue key = DecodeCore(data, ref position); BencodedValue value = DecodeCore(data, ref position); entries.Add(new BencodedEntry { Key = key, Value = value }); } position++; return(new BencodedValue { Data = new BencodedData(data, offset, position - offset), Dictionary = entries.ToArray() }); }
public static string[] AllTexts(this BencodedValue value, Encoding encoding) { return(AllTexts(new List <string>(), value, encoding).ToArray()); }
public static long ToInt64(this BencodedValue value) { return(value.Number.ToInt64()); }
public static int ToInt32(this BencodedValue value) { return(value.Number.ToInt32()); }
public static string ToText(this BencodedValue value, Encoding encoding) { return(AllTexts(value, encoding).FirstOrDefault()); }
public static string ToText(this BencodedValue value) { return(ToText(value, Encoding.ASCII)); }