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, and a reader that returns the tokens of a stream one at a time. See Reading bencode and Writing bencode.
- A torrent model for
.torrentfiles, 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:
| 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 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
- Getting started: install the package, read a torrent file and decode bencode data.
- Limitations: what the library does not do.