Bencoding encoder and decoder


/ Published in: C#
Save to your folder(s)

For those having trouble with encoding and decoding bencode, here is something to help you.


Copy this code and paste it in your HTML
  1. /*****
  2.   * Encoding usage:
  3.   *
  4.   * new BDictionary()
  5.   * {
  6.   * {"Some Key", "Some Value"},
  7.   * {"Another Key", 42}
  8.   * }.ToBencodedString();
  9.   *
  10.   * Decoding usage:
  11.   *
  12.   * BencodeDecoder.Decode("d8:Some Key10:Some Value13:Another Valuei42ee");
  13.   *
  14.   * Feel free to use it.
  15.   * More info about Bencoding at http://wiki.theory.org/BitTorrentSpecification#bencoding
  16.   *
  17.   * Originally posted at http://snipplr.com/view/37790/ by SuprDewd.
  18.   * */
  19.  
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24.  
  25. namespace Bencoding
  26. {
  27. /// <summary>
  28. /// A class used for decoding Bencoding.
  29. /// </summary>
  30. public static class BencodeDecoder
  31. {
  32. /// <summary>
  33. /// Decodes the string.
  34. /// </summary>
  35. /// <param name="bencodedString">The bencoded string.</param>
  36. /// <returns>An array of root elements.</returns>
  37. public static BElement[] Decode(string bencodedString)
  38. {
  39. int index = 0;
  40.  
  41. try
  42. {
  43. if (bencodedString == null) return null;
  44.  
  45. List<BElement> rootElements = new List<BElement>();
  46. while (bencodedString.Length > index)
  47. {
  48. rootElements.Add(ReadElement(ref bencodedString, ref index));
  49. }
  50.  
  51. return rootElements.ToArray();
  52. }
  53. catch (BencodingException) { throw; }
  54. catch (Exception e) { throw Error(e); }
  55. }
  56.  
  57. private static BElement ReadElement(ref string bencodedString, ref int index)
  58. {
  59. switch (bencodedString[index])
  60. {
  61. case '0':
  62. case '1':
  63. case '2':
  64. case '3':
  65. case '4':
  66. case '5':
  67. case '6':
  68. case '7':
  69. case '8':
  70. case '9': return ReadString(ref bencodedString, ref index);
  71. case 'i': return ReadInteger(ref bencodedString, ref index);
  72. case 'l': return ReadList(ref bencodedString, ref index);
  73. case 'd': return ReadDictionary(ref bencodedString, ref index);
  74. default: throw Error();
  75. }
  76. }
  77.  
  78. private static BDictionary ReadDictionary(ref string bencodedString, ref int index)
  79. {
  80. index++;
  81. BDictionary dict = new BDictionary();
  82.  
  83. try
  84. {
  85. while (bencodedString[index] != 'e')
  86. {
  87. BString K = ReadString(ref bencodedString, ref index);
  88. BElement V = ReadElement(ref bencodedString, ref index);
  89. dict.Add(K, V);
  90. }
  91. }
  92. catch (BencodingException) { throw; }
  93. catch (Exception e) { throw Error(e); }
  94.  
  95. index++;
  96. return dict;
  97. }
  98.  
  99. private static BList ReadList(ref string bencodedString, ref int index)
  100. {
  101. index++;
  102. BList lst = new BList();
  103.  
  104. try
  105. {
  106. while (bencodedString[index] != 'e')
  107. {
  108. lst.Add(ReadElement(ref bencodedString, ref index));
  109. }
  110. }
  111. catch (BencodingException) { throw; }
  112. catch (Exception e) { throw Error(e); }
  113.  
  114. index++;
  115. return lst;
  116. }
  117.  
  118. private static BInteger ReadInteger(ref string bencodedString, ref int index)
  119. {
  120. index++;
  121.  
  122. int end = bencodedString.IndexOf('e', index);
  123. if (end == -1) throw Error();
  124.  
  125. long integer;
  126.  
  127. try
  128. {
  129. integer = Convert.ToInt64(bencodedString.Substring(index, end - index));
  130. index = end + 1;
  131. }
  132. catch (Exception e) { throw Error(e); }
  133.  
  134. return new BInteger(integer);
  135. }
  136.  
  137. private static BString ReadString(ref string bencodedString, ref int index)
  138. {
  139. int length, colon;
  140.  
  141. try
  142. {
  143. colon = bencodedString.IndexOf(':', index);
  144. if (colon == -1) throw Error();
  145. length = Convert.ToInt32(bencodedString.Substring(index, colon - index));
  146. }
  147. catch (Exception e) { throw Error(e); }
  148.  
  149. index = colon + 1;
  150. int tmpIndex = index;
  151. index += length;
  152.  
  153. try
  154. {
  155. return new BString(bencodedString.Substring(tmpIndex, length));
  156. }
  157. catch (Exception e) { throw Error(e); }
  158. }
  159.  
  160. private static Exception Error(Exception e)
  161. {
  162. return new BencodingException("Bencoded string invalid.", e);
  163. }
  164.  
  165. private static Exception Error()
  166. {
  167. return new BencodingException("Bencoded string invalid.");
  168. }
  169. }
  170.  
  171. /// <summary>
  172. /// An interface for bencoded elements.
  173. /// </summary>
  174. public interface BElement
  175. {
  176. /// <summary>
  177. /// Generates the bencoded equivalent of the element.
  178. /// </summary>
  179. /// <returns>The bencoded equivalent of the element.</returns>
  180. string ToBencodedString();
  181.  
  182. /// <summary>
  183. /// Generates the bencoded equivalent of the element.
  184. /// </summary>
  185. /// <param name="u">The StringBuilder to append to.</param>
  186. /// <returns>The bencoded equivalent of the element.</returns>
  187. StringBuilder ToBencodedString(StringBuilder u);
  188. }
  189.  
  190. /// <summary>
  191. /// A bencode integer.
  192. /// </summary>
  193. public class BInteger : BElement, IComparable<BInteger>
  194. {
  195. /// <summary>
  196. /// Allows you to set an integer to a BInteger.
  197. /// </summary>
  198. /// <param name="i">The integer.</param>
  199. /// <returns>The BInteger.</returns>
  200. public static implicit operator BInteger(int i)
  201. {
  202. return new BInteger(i);
  203. }
  204.  
  205. /// <summary>
  206. /// The value of the bencoded integer.
  207. /// </summary>
  208. public long Value { get; set; }
  209.  
  210. /// <summary>
  211. /// The main constructor.
  212. /// </summary>
  213. /// <param name="value">The value of the bencoded integer.</param>
  214. public BInteger(long value)
  215. {
  216. this.Value = value;
  217. }
  218.  
  219. /// <summary>
  220. /// Generates the bencoded equivalent of the integer.
  221. /// </summary>
  222. /// <returns>The bencoded equivalent of the integer.</returns>
  223. public string ToBencodedString()
  224. {
  225. return this.ToBencodedString(new StringBuilder()).ToString();
  226. }
  227.  
  228. /// <summary>
  229. /// Generates the bencoded equivalent of the integer.
  230. /// </summary>
  231. /// <returns>The bencoded equivalent of the integer.</returns>
  232. public StringBuilder ToBencodedString(StringBuilder u)
  233. {
  234. if (u == null) u = new StringBuilder('i');
  235. else u.Append('i');
  236. return u.Append(Value.ToString()).Append('e');
  237. }
  238.  
  239. /// <see cref="Object.GetHashCode()"/>
  240. public override int GetHashCode()
  241. {
  242. return this.Value.GetHashCode();
  243. }
  244.  
  245. /// <summary>
  246. /// Int32.Equals(object)
  247. /// </summary>
  248. public override bool Equals(object obj)
  249. {
  250. try
  251. {
  252. return this.Value.Equals(((BInteger)obj).Value);
  253. }
  254. catch { return false; }
  255. }
  256.  
  257. /// <see cref="Object.ToString()"/>
  258. public override string ToString()
  259. {
  260. return this.Value.ToString();
  261. }
  262.  
  263. /// <see cref="IComparable.CompareTo(object)"/>
  264. public int CompareTo(BInteger other)
  265. {
  266. return this.Value.CompareTo(other.Value);
  267. }
  268. }
  269.  
  270. /// <summary>
  271. /// A bencode string.
  272. /// </summary>
  273. public class BString : BElement, IComparable<BString>
  274. {
  275. /// <summary>
  276. /// Allows you to set a string to a BString.
  277. /// </summary>
  278. /// <param name="s">The string.</param>
  279. /// <returns>The BString.</returns>
  280. public static implicit operator BString(string s)
  281. {
  282. return new BString(s);
  283. }
  284.  
  285. /// <summary>
  286. /// The value of the bencoded integer.
  287. /// </summary>
  288. public string Value { get; set; }
  289.  
  290. /// <summary>
  291. /// The main constructor.
  292. /// </summary>
  293. /// <param name="value"></param>
  294. public BString(string value)
  295. {
  296. this.Value = value;
  297. }
  298.  
  299. /// <summary>
  300. /// Generates the bencoded equivalent of the string.
  301. /// </summary>
  302. /// <returns>The bencoded equivalent of the string.</returns>
  303. public string ToBencodedString()
  304. {
  305. return this.ToBencodedString(new StringBuilder()).ToString();
  306. }
  307.  
  308. /// <summary>
  309. /// Generates the bencoded equivalent of the string.
  310. /// </summary>
  311. /// <param name="u">The StringBuilder to append to.</param>
  312. /// <returns>The bencoded equivalent of the string.</returns>
  313. public StringBuilder ToBencodedString(StringBuilder u)
  314. {
  315. if (u == null) u = new StringBuilder(this.Value.Length);
  316. else u.Append(this.Value.Length);
  317. return u.Append(':').Append(this.Value);
  318. }
  319.  
  320. /// <see cref="Object.GetHashCode()"/>
  321. public override int GetHashCode()
  322. {
  323. return this.Value.GetHashCode();
  324. }
  325.  
  326. /// <summary>
  327. /// String.Equals(object)
  328. /// </summary>
  329. public override bool Equals(object obj)
  330. {
  331. try
  332. {
  333. return this.Value.Equals(((BString)obj).Value);
  334. }
  335. catch { return false; }
  336. }
  337.  
  338. /// <see cref="Object.ToString()"/>
  339. public override string ToString()
  340. {
  341. return this.Value.ToString();
  342. }
  343.  
  344. /// <see cref="IComparable.CompareTo(Object)"/>
  345. public int CompareTo(BString other)
  346. {
  347. return this.Value.CompareTo(other.Value);
  348. }
  349. }
  350.  
  351. /// <summary>
  352. /// A bencode list.
  353. /// </summary>
  354. public class BList : List<BElement>, BElement
  355. {
  356. /// <summary>
  357. /// Generates the bencoded equivalent of the list.
  358. /// </summary>
  359. /// <returns>The bencoded equivalent of the list.</returns>
  360. public string ToBencodedString()
  361. {
  362. return this.ToBencodedString(new StringBuilder()).ToString();
  363. }
  364.  
  365. /// <summary>
  366. /// Generates the bencoded equivalent of the list.
  367. /// </summary>
  368. /// <param name="u">The StringBuilder to append to.</param>
  369. /// <returns>The bencoded equivalent of the list.</returns>
  370. public StringBuilder ToBencodedString(StringBuilder u)
  371. {
  372. if (u == null) u = new StringBuilder('l');
  373. else u.Append('l');
  374.  
  375. foreach (BElement element in base.ToArray())
  376. {
  377. element.ToBencodedString(u);
  378. }
  379.  
  380. return u.Append('e');
  381. }
  382.  
  383. /// <summary>
  384. /// Adds the specified value to the list.
  385. /// </summary>
  386. /// <param name="value">The specified value.</param>
  387. public void Add(string value)
  388. {
  389. base.Add(new BString(value));
  390. }
  391.  
  392. /// <summary>
  393. /// Adds the specified value to the list.
  394. /// </summary>
  395. /// <param name="value">The specified value.</param>
  396. public void Add(int value)
  397. {
  398. base.Add(new BInteger(value));
  399. }
  400. }
  401.  
  402. /// <summary>
  403. /// A bencode dictionary.
  404. /// </summary>
  405. public class BDictionary : SortedDictionary<BString, BElement>, BElement
  406. {
  407. /// <summary>
  408. /// Generates the bencoded equivalent of the dictionary.
  409. /// </summary>
  410. /// <returns>The bencoded equivalent of the dictionary.</returns>
  411. public string ToBencodedString()
  412. {
  413. return this.ToBencodedString(new StringBuilder()).ToString();
  414. }
  415.  
  416. /// <summary>
  417. /// Generates the bencoded equivalent of the dictionary.
  418. /// </summary>
  419. /// <param name="u">The StringBuilder to append to.</param>
  420. /// <returns>The bencoded equivalent of the dictionary.</returns>
  421. public StringBuilder ToBencodedString(StringBuilder u)
  422. {
  423. if (u == null) u = new StringBuilder('d');
  424. else u.Append('d');
  425.  
  426. for (int i = 0; i < base.Count; i++)
  427. {
  428. base.Keys.ElementAt(i).ToBencodedString(u);
  429. base.Values.ElementAt(i).ToBencodedString(u);
  430. }
  431.  
  432. return u.Append('e');
  433. }
  434.  
  435. /// <summary>
  436. /// Adds the specified key-value pair to the dictionary.
  437. /// </summary>
  438. /// <param name="key">The specified key.</param>
  439. /// <param name="value">The specified value.</param>
  440. public void Add(string key, BElement value)
  441. {
  442. base.Add(new BString(key), value);
  443. }
  444.  
  445. /// <summary>
  446. /// Adds the specified key-value pair to the dictionary.
  447. /// </summary>
  448. /// <param name="key">The specified key.</param>
  449. /// <param name="value">The specified value.</param>
  450. public void Add(string key, string value)
  451. {
  452. base.Add(new BString(key), new BString(value));
  453. }
  454.  
  455. /// <summary>
  456. /// Adds the specified key-value pair to the dictionary.
  457. /// </summary>
  458. /// <param name="key">The specified key.</param>
  459. /// <param name="value">The specified value.</param>
  460. public void Add(string key, int value)
  461. {
  462. base.Add(new BString(key), new BInteger(value));
  463. }
  464.  
  465. /// <summary>
  466. /// Gets or sets the value assosiated with the specified key.
  467. /// </summary>
  468. /// <param name="key">The key of the value to get or set.</param>
  469. /// <returns>The value assosiated with the specified key.</returns>
  470. public BElement this[string key]
  471. {
  472. get
  473. {
  474. return this[new BString(key)];
  475. }
  476. set
  477. {
  478. this[new BString(key)] = value;
  479. }
  480. }
  481. }
  482.  
  483. /// <summary>
  484. /// A bencoding exception.
  485. /// </summary>
  486. public class BencodingException : FormatException
  487. {
  488. /// <summary>
  489. /// Creates a new BencodingException.
  490. /// </summary>
  491. public BencodingException() { }
  492.  
  493. /// <summary>
  494. /// Creates a new BencodingException.
  495. /// </summary>
  496. /// <param name="message">The message.</param>
  497. public BencodingException(string message) : base(message) { }
  498.  
  499. /// <summary>
  500. /// Creates a new BencodingException.
  501. /// </summary>
  502. /// <param name="message">The message.</param>
  503. /// <param name="inner">The inner exception.</param>
  504. public BencodingException(string message, Exception inner) : base(message, inner) { }
  505. }
  506.  
  507. /// <summary>
  508. /// A class with extension methods for use with Bencoding.
  509. /// </summary>
  510. public static class BencodingExtensions
  511. {
  512. /// <summary>
  513. /// Decode the current instance.
  514. /// </summary>
  515. /// <param name="s">The current instance.</param>
  516. /// <returns>The root elements of the decoded string.</returns>
  517. public static BElement[] BDecode(this string s)
  518. {
  519. return BencodeDecoder.Decode(s);
  520. }
  521. }
  522. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.