Bencode

Bencode

Bencode (pronounced "Bee-Encode") is the encoding used by the peer-to-peer file sharing system BitTorrent for storing and transmitting loosely structured data.

It supports four different types of values:
* byte strings,
* integers,
* lists, and
* dictionaries (associative arrays).

Bencoding is most commonly used in .torrent files. These metadata files are simply bencoded dictionaries.

While less efficient than a pure binary encoding, bencoding is simple and (because numbers are encoded 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.

Encoding algorithm

Bencode uses ASCII characters as delimiters and digits.
* An integer is encoded as i"base 10 notation>"e. Note that negative values are allowed by prefixing the number with a minus sign, but leading zeros are not allowed (although the number zero is still represented as "0"). The number 42 would thus be encoded as "i42e".
* A byte string (a sequence of bytes, not necessarily characters) is encoded as "":"". (This is similar to netstrings, but without the final comma.) The length is encoded in base 10, like integers, but must be non-negative (zero is allowed); the contents are just the bytes that make up the string. The string "spam" would be encoded as "4:spam". The specification does not deal with encoding of characters outside the ASCII set; to mitigate this, some BitTorrent applications explicitly communicate the encoding (most commonly UTF-8) in various non-standard ways.
* A list of values is encoded as l""e . The contents consist of the bencoded elements of the list, in order, concatenated. A list consisting of the string "spam" and the number 42 would be encoded as: "l4:spami42ee"; note the absence of separators between elements.
* A dictionary is encoded as d""e. The elements of the dictionary are again encoded and concatenated, in such a way that each value immediately follows the key associated with it. All keys must be byte strings and must appear in lexicographical order. A dictionary that associates the values 42 and "spam" with the keys "foo" and "bar", respectively, would be encoded as follows: "d3:bar4:spam3:fooi42ee". (This might be easier to read by inserting some spaces: nowrap|"d 3:bar 4:spam 3:foo i42e e".)There are no restrictions on what kind of values may be stored in lists and dictionaries; they may (and usually do) contain other lists and dictionaries. This allows for arbitrarily complex data structures to be encoded; it's one of the advantages of using bencoding.

Features

* For each possible (complex) value, there is only a single valid bencoding; ie. there is a bijection between values and their encodings. This has the advantage that applications may compare bencoded values by comparing their encoded forms, eliminating the need to decode the values.
* Many encodings can be decoded manually, but since the bencoded values often contain binary data, and may become quite complex, it is generally not considered a human-readable encoding.
* Bencoding serves similar purposes as markup languages like XML and JSON, allowing complex yet loosely structured data to be stored in a platform independent way.

ee also

*BitTorrent

External links

* [http://wiki.theory.org/BitTorrentSpecification Another BitTorrent protocol specification]
* [http://www.pear.php.net/package/File_Bittorrent2/ File_Bittorrent2] - Another PHP Bencode/decode implementation
* [http://pypi.python.org/pypi/BitTorrent-bencode/5.0.8 The original BitTorrent implementation in Python as standalone package]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Bencode — Bencode  это формат, используемый p2p системой распространения файлов BitTorrent для хранения и передачи свободно структурированных данных. Включает в себя четыре типа данных: строка байт, целое число, список (массив), словарь. Bencode… …   Википедия

  • BitTorrent — Эта статья о протоколе. Статья о клиенте: BitTorrent (программа). BitTórrent (букв. англ.  «битовый поток»)  пиринговый (P2P) сетевой протокол для кооперативного обмена файлами через Интернет. Файлы передаются частями, каждый torrent… …   Википедия

  • .torrent — Файл метаданных является словарём в bencode формате с расширением .torrent  используется в p2p сети BitTorrent и содержит информацию о файлах, трекерах и др. Описание Содержит следующую информацию: URL трекера; Общую информацию о файлах (имя …   Википедия

  • BitTorrent (protocol) — BitTorrent is a peer to peer file sharing protocol used to distribute large amounts of data. The initial distributor of the complete file or collection acts as the first seed . Each peer who downloads the data also uploads them to other peers.… …   Wikipedia

  • Comparison of remote desktop software — This page is a comparison of remote desktop software available for various platforms. Contents 1 Remote desktop software 1.1 General information 1.2 Operating system support 1.3 Featu …   Wikipedia

  • Netstrings — In computer programming, a netstring refers to a self delimited way of encoding a (byte)string, defined in a document by D. J. Bernstein.Netstrings store the byte length data that follows, making it easier to unambiguously pass text and byte data …   Wikipedia

  • BitTorrent-трекер — BitTorrent трекер  сервер, осуществляющий координацию клиентов BitTorrent. Содержание 1 Координация клиентов 2 Роль трекера …   Википедия

  • Comparison of data serialization formats — This is a comparison of data serialization formats, different ways to convert complex objects to sequences of bits. It does not include markup languages used exclusively as document file formats. Contents 1 Overview 2 Syntax comparison of human… …   Wikipedia

  • Netstring — In computer programming, a netstring is a formatting method for byte strings that uses a declarative notation to indicate the size of the string. [1][2] Netstrings store the byte length of the data that follows, making it easier to unambiguously… …   Wikipedia

  • Сравнение систем удалённого доступа — …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”