/* Bencode.cs * * Changelog: * Jan. 14, 2016: Example code * 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); } } } class BencodeExample1 { int idx; byte [] buff; BencodeExample1 () { idx = 0; buff = new byte [1024]; } private byte getc () { Console.WriteLine (String.Format ("getc: idx is {0}", idx)); return buff[idx++]; } private void putc (byte b) { Console.WriteLine (String.Format ("putc: idx is {0}", idx)); buff[idx++] = b; } public static void Example1 () { BencodeExample1 ex1 = new BencodeExample1 (); Bencode codec = new Bencode (ex1.getc, ex1.putc); codec.Encode (789); PrintBytes (ex1.buff, 0, 5); codec.Encode ((object) new byte [] {(byte) 'a', (byte) 'b'}); PrintBytes (ex1.buff, 5, 4); codec.Encode ((object) "César"); PrintBytes (ex1.buff, 9, 8); SortedList l = new SortedList (); // 2 l.Add ("x", "111"); // 3 + 5 l.Add ("d", 2); // 3 + 3 codec.Encode ((object) l); // 16 PrintBytes (ex1.buff, 17, 16); ArrayList a = new ArrayList (); // 2 a.Add ("Hola"); // 6 a.Add ((object) 123); // 5 codec.Encode (a); // 13 PrintBytes (ex1.buff, 33, 13); Console.WriteLine ("\n\nAll right, decoding this buffer:"); Console.WriteLine ("There should be 5 objects"); PrintBytes (ex1.buff, 0, 100); object obj; ex1.idx = 0; Console.WriteLine ("\nInteger 789:"); obj = codec.Decode (); // 789 Console.WriteLine (obj.GetType ()); Console.WriteLine ((int) obj); Console.WriteLine ("\nArray of length 2 (bytes 'a' and 'b')"); obj = codec.Decode (); // array Console.WriteLine (obj.GetType ()); Console.WriteLine ((char) ((byte []) obj)[0]); Console.WriteLine ((char) ((byte []) obj)[1]); Console.WriteLine ("\nstring 'César' (with accents!)"); obj = codec.Decode (); // string César Console.WriteLine (obj.GetType ()); Console.WriteLine (Encoding.UTF8.GetString ((byte []) obj)); Console.WriteLine ("\ndictionary with 2 keys"); obj = codec.Decode (); // dictionary with 2 keys Console.WriteLine (obj.GetType ()); foreach (DictionaryEntry e in (SortedList) obj) { Console.WriteLine (e.Key); Console.WriteLine (e.Value); } Console.WriteLine ("\narray with 2 elements"); obj = codec.Decode (); // array list with 2 items Console.WriteLine (obj.GetType ()); foreach (object o in (ArrayList) obj) { Console.WriteLine (o); } Console.WriteLine ("\nthe entire buffer seen as a string:"); Console.WriteLine (Encoding.UTF8.GetString (ex1.buff)); } static public void PrintBytes (byte [] a, int offset, int len) { for (int i = 0; i < len; i++) Console.Write ("0x{0:x} ({1}) ", a[offset+i], (char) a[offset+i]); Console.WriteLine (); } } /* public class Program { static public void Main (string [] args) { BencodeExample1.Example1 (); } } */ } // vim: set efm=%f(%l\\,%c)\:\ %m: