Jump to content

Bencode

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Omkarsingh2345 (talk | contribs) at 17:00, 22 January 2022 (100 FAMOUS STORIES). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

There was once a sweet little girl who lived in a pretty little cottage at the end of the village. She always wore a red cloak with a hood, so people called her Little Red Riding Hood. One morning, Little Red Riding Hood\u2019s mother said, “Put on your things and go to see your grandmother. She has been ill. Take along this basket with food for her.“

It was a bright and sunny morning so Little Red Riding Hood stopped to pick a bunch of flowers for her grandmother. Little Red Riding Hood wandered from her path, when from behind her a gruff voice said, “ Good morning, Little Red Riding Hood!“

Little Red Riding Hood turned around and saw a great big wolf. “ Where are you going Little Red Riding Hood?“


“I am going to my grandmother, who is ill, Mr. Wolf.“

Then Mr. Wolf said, “Good morning“ and set off. After walking for a while, the wolf reached Little Red Riding Hood‘s grandmother‘s house. He opened the door and walked in, where the grandmother lay in bed. He made one jump at her but she jumped out of bed and into a closet. So the wolf put on the cap which she had dropped and crept under the bedclothes. In a short while, Little Red Riding Hood knocked at the door and walked in saying, “Good morning grandmother! I have bought you food! And here is a bunch of flowers I gathered in the wood.“

As she came nearer to the bed, she said, “Why grandmother! What a big mouth you have!“


“All the better to eat you up with, my dear“ he said, as he sprang at Little Red Riding Hood. Just at that moment, two hunters were passing by the cottage and heard her scream. They rushed in and killed the wolf, and everybody was happy that Little Red Riding Hood had been saved, and Little Red Riding Hood learnt her lesson.

Encoding algorithm

Bencode uses ASCII characters as delimiters and digits.

  • An integer is encoded as i<integer encoded in base ten ASCII>e. Leading zeros are not allowed (although the number zero is still represented as "0"). Negative values are encoded by prefixing the number with a hyphen-minus. The number 42 would thus be encoded as i42e, 0 as i0e, and -42 as i-42e. Negative zero is not permitted.
  • A byte string (a sequence of bytes, not necessarily characters) is encoded as <length>:<contents>. 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. This is identical to how netstrings work, except that netstrings additionally append a comma suffix after the byte sequence.
  • A list of values is encoded as l<contents>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, and the first character is the letter 'l', not digit '1'.
  • A dictionary is encoded as d<contents>e. The elements of the dictionary are encoded with each key immediately followed by its value. 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 (in other words, {"bar": "spam", "foo": 42}), would be encoded as follows: d3:bar4:spam3:fooi42ee.

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.

Features & drawbacks

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.
  • Many BE codegroups can be decoded manually. Since the bencoded values often contain binary data, decoding may become quite complex. Bencode is not considered a human-readable encoding format.
  • 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.

However, this uniqueness can cause some problems:

  • There are very few bencode editors[1]
  • 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.

See also

References

  1. ^ "BEncode Editor". μTorrent Community Forums. Retrieved 24 October 2014.