请问如何用php解析BT种子的bencoded编码

「已注销」 2005-09-23 08:24:45
d8:announce36:http://btfans.3322.org:8000/announce13:announce-listll36:http://btfans.3322.org:8000/announce76:http://btfans.3322.org:8000/announceudp://tracker.bitcomet.net:8080/announce36:http://btfans.3322.org:8080/announce36:http://btfans.3322.org:6969/announceee13:creation datei1127376448e8:encoding3:GBK4:infod6:lengthi292712199e4:name13:cmr4setup.exe10:name.utf-813:cmr4setup.exe12:piece lengthi262144e6:pieces22340:8

BT种子文件使用了一种叫bencoding的编码方法来保存数据。
bencoding现有四种类型的数据:srings(字符串),integers(整数),lists(列表),dictionaries(字典)
编码规则如下:
strings(字符串)编码为:<字符串长度>:<字符串>
例如: 4:test 表示为字符串"test"
4:例子 表示为字符串“例子”
字符串长度单位为字节
没开始或结束标记

integers(整数)编码为:i<整数>e
开始标记i,结束标记为e
例如: i12345e 表示为整数12345
i-12345e 表示为整数-12345
整数没有大小限制
i0e 表示为整数0
lists(列表)编码为:l<bencoding编码类型>e
开始标记为l,结束标记为e
列表里可以包含任何bencoding编码类型,包括整数,字符串,列表,字典。
例如: l4:test5abcdee 表示为二个字符串["test","abcde"]

dictionaries(字典)编码为d<bencoding字符串><bencoding编码类型>e
开始标记为d,结束标记为e
关键字必须为bencoding字符串
值可以为任何bencoding编码类型
例如: d3:agei20ee 表示为{"age"=20}
d4:path3:C:\8:filename8:test.txte 表示为{"path"="C:\","filename"="test.txt"}

---------------------------------------------``````````````````````````````
问题
1.应该怎么提取出,比如d4:path3:C:\8:filename8:test.txte
2.如果提取出d4:path3:C:\8:filename8:test.txte,之后应该怎么进一步分析出4:path是字符串path。

谢谢
...全文
1009 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2005-09-23
  • 打赏
  • 举报
回复
我下载的bt整站 全部都让ZEND给加密了 郁闷的够呛,已经想了好久又看说明文档又查书的实在不行才上来发帖子问的,真的很谢谢
xuzuning 2005-09-23
  • 打赏
  • 举报
回复
Array
(
[0] => Array
(
[type] => dictionaries
[0] => Array
(
[key] => Array
(
[type] => strings
[value] => path
)

[value] => Array
(
[type] => strings
[value] => C:\
)

)

[1] => Array
(
[key] => Array
(
[type] => strings
[value] => filename
)

[value] => Array
(
[type] => strings
[value] => test.txt
)

)

)

)
xuzuning 2005-09-23
  • 打赏
  • 举报
回复
class bencoded {
var $data = array();
var $buffer = '';
var $sp = 0;
function getchar() {
if($this->sp == strlen($this->buffer))
return false;
$ch = $this->buffer[$this->sp];
$this->sp++;
return $ch;
}
function getword() {
switch($ch = $this->getchar()) {
case false:
return false;
case 'i':
return $this->integers(); //整数
case 'l':
return $this->lists(); //列表
case 'd':
return $this->dictionaries(); //字典
default:
return $this->strings($ch); //字符串
}
}
function decode($in) {
$this->buffer = $in;
$sp = 0;
while(($t = $this->getword()) !== false)
$this->data[] = $t;
print_r($this->data);
}
function integers() {
$t = '';
while(($ch = $this->getchar()) != 'e')
$t .= $ch;
return array('type' => 'integers', 'value' => $t);
}
function strings($n=0) {
while(($ch = $this->getchar()) != ':')
$n .= $ch;
$t = '';
for($i=0;$i<$n;$i++)
$t .= $this->getchar();
return array('type' => 'strings', 'value' => $t);
}
function lists() {
$t = array('type' => 'lists');
while($this->buffer[$this->sp] != 'e')
$t[] = $this->getword();
$this->getchar();
return $t;
}
function dictionaries() {
$t = array('type' => 'dictionaries');
while($this->buffer[$this->sp] != 'e')
$t[] = array('key' => $this->strings(), 'value' => $this->getword());
$this->getchar();
return $t;
}
}

$o = new bencoded;
$bt = 'd4:path3:C:\8:filename8:test.txte';
$o->decode($bt);

注:省略了编码方法

zairwolfo 2005-09-23
  • 打赏
  • 举报
回复
去上网下载一些现成的代码看看。这种bt整站很多。

21,882

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧