BEncoding 1.0

Published By: Glass Echidna
License Type: Freeware
Date Added: 23 July, 2013
Report Error Link

Category: Development / Other
Description of BEncoding program at Mac Shareware:



I recently found my interest piqued by the open-source, cross-platform BitTorrent client, Transmission.

I recently found my interest piqued by the open-source, cross-platform BitTorrent client, Transmission. Upon downloading the source code for Transmission, we were disappointed to see that libtransmission featured only a very low-level BEncoding implementation. The unofficial BitTorrent specification refers to implementations in several languages, including Objective-C.The Objective-C implementation provides a nice high-level library of methods (in reality, only two are needed!), but has a few flaws: there are some memory leaks, it doesn't quite implement the specification entirely correctly and string-handling functionality is cumbersome. We've improved upon it in these respects and are releasing the code after the jump.The primary issue that motivated us to rewrite the provided Objective-C implementation is the handling of dictionaries. This is what the unofficial specification has to say on the matter (emphasis ours) : The initial d and trailing e are the beginning and ending delimiters. Note that the keys must be bencoded strings. The values may be any bencoded type, including integers, strings, lists, and other dictionaries. Keys must be strings and appear in sorted order (sorted as raw strings, not alphanumerics). The strings should be compared using a binary comparison, not a culture-specific 'natural' comparison.The Objective-C implementation fails to adhere to the bolded clause. This is because it provides support for dictionaries by means of Cocoa's NSDictionary class cluster. Luckily, fixing this issue is as easy as using the OrderedDictionary class as a drop-in replacement. This takes care of instantiating order-aware dictionaries from bencoded data. Providing support for going the other way (from a dictionary to its bencoded equivalent) is similarly straight-forward.Once this was fixed, we moved onto the issue of memory leaks. We were not the only ones to notice these leaks, so we pulled out our static analyser and got to work. For internal code consistency, we opted to replace leaking manually-allocated objects with autoreleased counterparts. This also resulted in slightly more concise code than allocating and releasing everything by hand.Once we had finally arrived at a compliant implementation, we turned our attention towards the issue of string handling. The blog post to which the Obj-C implementation was attached noted that there is no hint as to whether a bencoded byte string is a human-readable string or a binary blob. Rather than leaving everything as NSData instances, we have developed another method for the BEncoding class that takes a 'type advisor' block as an argument. Whenever a byte string is encountered, the user-provided block is called and passed the current dictionary key 'stack' for the data being processed. This block can then 'hint' to the decoding routine whether it should be processed as a string or a byte stream. An example of this is illustrated below.

BEncoding is a completely free software. You can free download and use it as long as you like.
License: Freeware
Downloads: 0
Size: 20.5 KB

Platform: Mac OS X, Mac

System Requirements: ?’A· Apple Xcode