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, and a reader that returns the tokens of a stream one at a time. See Reading bencode and Writing bencode.
  • A torrent model for .torrent files, BitTorrent v1, v2 and hybrid: the trackers, the files and the piece hashes as typed properties, and the info-hashes that identify 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 and BencodeReader are in the Meziantou.Framework.Bencode namespace. The torrent model (TorrentFile, TorrentInfo, TorrentInfoFile, TorrentFileTreeEntry, TorrentPieceLayer, TorrentNode) is in Meziantou.Framework.Bencode.Torrent.

Next steps