Introduction
editMeziantou.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
.torrentfiles: 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:
| Kind | Encoding | Example | Type |
|---|---|---|---|
| Integer | i<digits>e | i42e | BencodeInteger |
| String | <length>:<bytes> | 4:spam | BencodeString |
| List | l<values>e | l4:spami42ee | BencodeList |
| Dictionary | d<key><value>…e | d3:cow3:mooe | BencodeDictionary |
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
- Getting started: install the package, read a torrent file and decode bencode data.
- Limitations: what the library does not do.