Bencode

Bencode(發音為Bee-Encode)是BitTorrent用在傳輸資料結構的編碼方式。這種編碼方式支援四種資料型態:

Bencode最常被用在.torrent檔中,檔案裡的元數據都是被Bencode编码过的字典表。这种编码方法也被Tracker返回响应时使用。

虽然比用纯二进制编码效率低,但Bencode结构简单而且不受字节存储顺序影响(所有数字以十进制编码),这对于跨平台性非常重要。并且,Bencode具有较好的灵活性,即使存在故障的字典键,只要将其忽略并更换新的就能兼容补充。

编码方法

Bencode使用ASCII字符进行编码。

  • 一个整型数会以十进制数编码并括在i和e之间,不允许前导零(但0依然为整数0),负数如十进制表示一样使用前导负号,不允许负零。如整型数“42”编码为“i42e”,数字“0”编码为“i0e”,“-42”编码为“i-42e”。
  • 一个以字节为单位表示的字符串(字符串的字为一个字节,不一定是一个字符)会以(长度):(内容)编码,长度的值和数字编码方法一样,只是不允许负数;内容就是字符串的内容,如字符串“spam”就会编码为“4:spam”,本规则不能处理ASCII以外的字符串,为了解决这个问题,一些BitTorrent程序会以非标准的方式将ASCII以外的字符以UTF-8编码转化后再编码。
  • 线性表会以l和e括住来编码,其中的内容为Bencode四种编码格式所组成的编码字串,如包含和字符串“spam”数字“42”的线性表会被编码为“l4:spami42ee”,注意分隔符要对应配对。
  • 字典表会以d和e括住来编码,字典元素的键和值必须紧跟在一起,而且所有键为字符串类型并按字典顺序排好。如键为“bar”值为字串“spam”和鍵为“foo”值为整數“42”的字典表会被编码为“d3:bar4:spam3:fooi42ee”。

对于线性表和字典的取值范围并没有限制,它们通常会包含其他元素,这样就允许对很复杂的数据结构进行编码。

外部連結

  • Another BitTorrent protocol specification(页面存档备份,存于互联网档案馆
  • File_Bittorrent2(页面存档备份,存于互联网档案馆) - Another PHP Bencode/decode implementation
  • The original BitTorrent implementation in Python as standalone package(页面存档备份,存于互联网档案馆
  • BEncode Editor
  • bencode-tools(页面存档备份,存于互联网档案馆) - a C library for manipulating bencoded data and a XML schema like validator for bencode messages in Python
  • Bencode parsing in Java
  • Bencode parsing in C
BitTorrent协议(分类)
公司
BitTorrent公司 · Vuze公司英语Vuze
人士
布莱姆·科亨 · Ashwin Navin英语Ashwin Navin
技术
BitTorrent协议 · Broadcatching英语Broadcatching · 分布式散列表(DHT) · DNA英语BitTorrent DNA · BitTorrent索引英语BitTorrent#Indexing · 节点交换 · 协议加密 · 超级种子 · Tracker · 种子文件 · 磁力链接  · µTP · PT下载 · Bencode · 用语(BitTorrent)英语Glossary of BitTorrent terms
客户端
列表
BitTorrent(原版) · KTorrent · LimeWire · MLDonkey · Shareaza · Transmission · rTorrent · µTorrent · Vuze(原名Azureus) · Deluge · OneSwarm · Opera · Lphant · BitComet · 比特精灵 · 迅雷 · 脱兔 · 快车 · 哇嘎 · BitTornado英语BitTornado · qBittorrent · Free Download Manager
Tracker软件
opentracker · PeerTracker英语PeerTracker · XBT Tracker英语XBT Tracker
网站
其他
MPAA · aXXo英语aXXo · BitTorrent Open Source License英语BitTorrent Open Source License · Peer Media Technologies英语Peer Media Technologies · TorrentFreak英语TorrentFreak · Slyck.com英语Slyck.com