/* Bencode.cs * Version: Dec. 4, 2014 * * Changelog: * Dec. 4, 2014: Support for ArrayList coding / decoding * Dec. 2, 2014: Initial version */ using System; using System.Text; using System.Diagnostics; using System.Collections; using System.Collections.Generic; namespace RIPS { public class Bencode { private static readonly Encoding utf8codec = System.Text.Encoding.UTF8; private GetcDelegate Getc; private PutcDelegate Putc; public delegate byte GetcDelegate (); public delegate void PutcDelegate (byte b); public Bencode (GetcDelegate g, PutcDelegate p) { Getc = g; Putc = p; } public void Encode (int x) { foreach (char c in utf8codec.GetBytes (String.Format ("i{0}e", x))) { Putc ((byte) c); } } public void Encode (byte [] buff) { byte [] pref = utf8codec.GetBytes (String.Format ("{0}:", buff.Length)); foreach (byte b in pref) Putc (b); foreach (byte b in buff) Putc (b); } public void Encode (string s) { Encode (utf8codec.GetBytes (s)); } public void Encode (ArrayList l) { Putc ((byte) 'l'); foreach (object obj in l) Encode (obj); Putc ((byte) 'e'); } public void Encode (SortedList d) { // a dictionary starts with 'd' Putc ((byte) 'd'); foreach (DictionaryEntry e in d) { // encode each (string) key if (e.Key.GetType () != typeof (string)) throw new Exception ("Keys must be of string type"); Encode ((string) e.Key); Encode (e.Value); } Putc ((byte) 'e'); } public void Encode (object obj) { if (obj.GetType () == typeof (int)) { Encode ((int) obj); } else if (obj.GetType () == typeof (string)) { Encode ((string) obj); } else if (obj.GetType () == typeof (byte [])) { Encode ((byte []) obj); } else if (obj.GetType () == typeof (ArrayList)) { Encode ((ArrayList) obj); } else if (obj.GetType () == typeof (SortedList)) { Encode ((SortedList) obj); } else { string s; s = String.Format ("Bencode doesn't know how to encode " + "values of type {0}", obj.GetType()); throw new Exception (s); } } public object Decode () { return __Decode ((char) Getc ()); } private object __Decode (char c) { string s = ""; int i, len; /* decode an integer */ if (c == 'i') { while (true) { c = (char) Getc (); if (c == 'e') break; s += c; } return Convert.ToInt32 (s); /* decode a sequence of bytes */ } else if (Char.IsDigit (c)) { s = c.ToString (); while (true) { c = (char) Getc (); if (c == ':') break; s += c; } len = Convert.ToInt32 (s); List buff = new List (); for (i = 0; i < len; i++) buff.Add (Getc ()); return buff.ToArray (); /* decode a list (recursively) */ } else if (c == 'l') { ArrayList l = new ArrayList (); while (true) { c = (char) Getc (); if (c == 'e') return l; l.Add (__Decode(c)); } /* decode a dictionary (recursively) */ } else if (c == 'd') { SortedList d = new SortedList (); while (true) { c = (char) Getc (); if (c == 'e') return d; d.Add (utf8codec.GetString ((byte []) __Decode(c)), Decode ()); } } else { s = String.Format ("Decoding error: Bencode expected " + "'i', 'd', or a digit, but character '{0}' was found", c); throw new Exception (s); } } } } // vim: set efm=%f(%l\\,%c)\:\ %m: