Bencode (original) (raw)

From Wikipedia, the free encyclopedia

Data serialization format

Bencode (pronounced like Bee-encode) is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data.[1]

It supports four different types of values:

Bencoding is most commonly used in torrent files, and as such is part of the BitTorrent specification. These metadata files are simply bencoded dictionaries.

Bencoding is simple and (because numbers are encoded as text in decimal notation) is unaffected by endianness, which is important for a cross-platform application like BitTorrent. It is also fairly flexible, as long as applications ignore unexpected dictionary keys, so that new ones can be added without creating incompatibilities.

Bencode uses ASCII characters as delimiters and digits to encode data structures in a simple and compact format.

There are no restrictions on the types of values stored within lists and dictionaries; they may contain other lists and dictionaries, allowing for arbitrarily complex data structures.

Types of Errors in Bencode

[edit]

Here is the list of the possible errors that a ill-formatted bencode may have:

  1. Null root value.
  2. Non-singular root item.
  3. Invalid type encountered (character not 'i', 'l', 'd', or '0'-'9').
  4. Missing 'e' terminator for 'i', 'l', or 'd' types.
  5. Integer errors:
    1. Contains non-digit characters.
    2. Has a leading zero.
    3. Is negative zero.
  6. Byte String errors:
    1. Negative length.
    2. Length not followed by ':'.
    3. Unexpected EOF before completing string.
  7. Dictionary errors:
    1. Key is not a string.
    2. Duplicate keys.
    3. Keys not sorted.
    4. Missing value for a key.

Bencode is a very specialized kind of binary coding with some unique properties:

However, this uniqueness can cause some problems:

  1. ^ The BitTorrent Protocol Specification Archived 2019-07-26 at the Wayback Machine. BitTorrent.org. Retrieved 8 October 2018.
  2. ^ "BEncode Editor". μTorrent Community Forums. 8 October 2007. Archived from the original on 24 October 2014. Retrieved 24 October 2014.