Bencode: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>Luavixen
m →Drawbacks: Fix incorrect placement of last two bullet points
 
imported>Thüringer
m →top: missing space
 
Line 5: Line 5:
* [[string (computer science)|byte strings]],
* [[string (computer science)|byte strings]],
* [[integer]]s,
* [[integer]]s,
* [[List (computing)|list]]s, and
* [[List (computing)|list]]s (arrays), and
* [[associative array|dictionaries (associative arrays)]].
* [[associative array|dictionaries (associative arrays)]].


Line 28: Line 28:
** A colon (<code>:</code>) separates the length and the contents.
** A colon (<code>:</code>) separates the length and the contents.
** The contents are the exact number of bytes specified by the length.
** The contents are the exact number of bytes specified by the length.
** The contents are a sequence of bytes (not a textual string)
** Examples:
** Examples:
*** An empty string is encoded as <code>0:</code>.
*** An empty string is encoded as <code>0:</code>.
Line 50: Line 51:
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.
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 ===
Bencode defines only byte string types, rather than any particular [[character encoding]] for storing text. Downstream applications and data format specifications that use bencode are free to specify whichever encoding they prefer for encoding text into bencoded byte strings.
 
=== Types of errors in Bencode ===


Here is the list of the possible errors that a ill-formatted bencode may have:
Here is the list of the possible errors that a ill-formatted bencode may have:
Line 62: Line 65:
## Has a leading zero.
## Has a leading zero.
## Is negative zero.
## Is negative zero.
# '''Byte String errors:'''
# '''Byte string errors:'''
## Negative length.
## Negative length.
## Length not followed by ':'.
## Length not followed by ':'.
## Unexpected EOF before completing string.
## Unexpected EOF before completing string.
## Length specified in units of codepoints (characters) rather than bytes.
# '''Dictionary errors:'''
# '''Dictionary errors:'''
## Key is not a string.
## Key is not a string.
## Duplicate keys.
## Duplicate keys.
## Keys not sorted.
## Keys not sorted.
## Keys incorrectly sorted by codepoint in a particular character encoding, rather than lexicographically sorted by ordinal.
## Missing value for a key.
## Missing value for a key.


Line 80: Line 85:
== Drawbacks ==
== Drawbacks ==


However, this uniqueness can cause some problems:
Bencode is not considered a [[human-readable]] encoding format. While the BE codegroups can be decoded manually, the bencoded values often contain [[Binary file|binary data]], so decoding by hand may be error prone. It is not safe to edit bencode files in text editors because bencoded files contain binary data, so a [[hex editor]] or specialised bencode editor tool must be used.


* There are very few bencode editors<ref>{{cite web | url = https://forum.utorrent.com/topic/26674-bencode-editor | title = BEncode Editor | date = 8 October 2007 | publisher = μTorrent Community Forums | access-date = 24 October 2014 | archive-date = 24 October 2014 | archive-url = https://web.archive.org/web/20141024131504/http://forum.utorrent.com/topic/26674-bencode-editor/ | url-status = live }}</ref>
Bencode does not store any metadata about the size of list or dictionary structures, requiring all preceding elements to be read sequentially in order to reach a particular field. As such, bencode may not be suitable for large data structures where [[random access]] to fields is required.
* Because bencoded files contain binary data, and because of some of the intricacies involved in the way binary strings are typically stored, it is often not safe to edit bencode files in text editors.
* Many BE codegroups can be decoded manually. Since the bencoded values often contain [[Binary file|binary data]], decoding may become quite complex. Bencode is not considered a [[human-readable]] encoding format.
* The specification deals with encoding characters in the ASCII set only leaving the solution to users. This brings about several solutions and less conformity.


==See also==
==See also==

Latest revision as of 07:36, 17 September 2025

Template:Short description 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.

Encoding Algorithm

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

  • Integers are encoded as i<base10 integer>e.
    • The integer is encoded in base 10 and may be negative (indicated by a leading hyphen-minus).
    • Leading zeros are not allowed unless the integer is zero.
    • Examples:
      • Zero is encoded as i0e.
      • The number 42 is encoded as i42e.
      • Negative forty-two is encoded as i-42e.
  • Byte Strings are encoded as <length>:<contents>.
    • The length is the number of bytes in the string, encoded in base 10.
    • A colon (:) separates the length and the contents.
    • The contents are the exact number of bytes specified by the length.
    • The contents are a sequence of bytes (not a textual string)
    • Examples:
      • An empty string is encoded as 0:.
      • The string "bencode" is encoded as 7:bencode.
  • Lists are encoded as l<elements>e.
    • Begins with l and ends with e.
    • Elements are bencoded values concatenated without delimiters.
    • Examples:
      • An empty list is encoded as le.
      • A list containing the string "bencode" and the integer -20 is encoded as l7:bencodei-20ee.
  • Dictionaries are encoded as d<pairs>e.
    • Begins with d and ends with e.
    • Contains key-value pairs.
    • Keys are byte strings and must appear in lexicographical order.
    • Each key is immediately followed by its value, which can be any bencoded type.
    • Examples:
      • An empty dictionary is encoded as de.
      • A dictionary with keys "wiki" → "bencode" and "meaning" → 42 is encoded as d7:meaningi42e4:wiki7:bencodee.

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.

Bencode defines only byte string types, rather than any particular character encoding for storing text. Downstream applications and data format specifications that use bencode are free to specify whichever encoding they prefer for encoding text into bencoded byte strings.

Types of errors in Bencode

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.
    4. Length specified in units of codepoints (characters) rather than bytes.
  7. Dictionary errors:
    1. Key is not a string.
    2. Duplicate keys.
    3. Keys not sorted.
    4. Keys incorrectly sorted by codepoint in a particular character encoding, rather than lexicographically sorted by ordinal.
    5. Missing value for a key.

Features

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

  • For each possible (complex) value, there is only a single valid bencoding; i.e. 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.
  • Bencoding serves similar purposes as data languages like JSON and YAML, allowing complex yet loosely structured data to be stored in a platform independent way. This allowing a linear memory storage for complex data.

Drawbacks

Bencode is not considered a human-readable encoding format. While the BE codegroups can be decoded manually, the bencoded values often contain binary data, so decoding by hand may be error prone. It is not safe to edit bencode files in text editors because bencoded files contain binary data, so a hex editor or specialised bencode editor tool must be used.

Bencode does not store any metadata about the size of list or dictionary structures, requiring all preceding elements to be read sequentially in order to reach a particular field. As such, bencode may not be suitable for large data structures where random access to fields is required.

See also

References

<templatestyles src="Reflist/styles.css" />

  1. ↑ The BitTorrent Protocol Specification Script error: No such module "webarchive".. BitTorrent.org. Retrieved 8 October 2018.

Script error: No such module "Check for unknown parameters".

External links