Introduction

edit

Meziantou.Framework.Bencode reads and writes bencode, the binary encoding that BitTorrent uses for .torrent files and for the messages of its protocols. It has two levels:

  • A document model for any bencode data: integers, byte strings, lists and dictionaries that you read, build and encode. See Reading bencode and Writing bencode.
  • A torrent model for .torrent files: the trackers, the files and the piece hashes as typed properties, and the info-hash that identifies the torrent. See Torrent files.

Bencode in short

Bencode has four kinds of values:

KindEncodingExampleType
Integeri<digits>ei42eBencodeInteger
String<length>:<bytes>4:spamBencodeString
Listl<values>el4:spami42eeBencodeList
Dictionaryd<key><value>…ed3:cow3:mooeBencodeDictionary

A string is a sequence of bytes, not necessarily text: the piece hashes of a torrent are binary. Dictionary keys are strings, and the canonical encoding sorts them by their bytes, so a given dictionary always encodes to the same bytes.

Namespaces

The document model is in the Meziantou.Framework.Bencode namespace. The torrent model (TorrentFile, TorrentInfo, TorrentInfoFile, TorrentNode) is in Meziantou.Framework.Bencode.Torrent.

Next steps