php bdecode,BDecode.php

class BDecode {

function numberdecode($wholefile, $offset) {

// Funky handling of negative numbers and zero

$negative = false;

if ($wholefile[$offset] == '-') {

$negative = true;

$offset++;

}

if ($wholefile[$offset] == '0') {

$offset++;

if ($negative)

return array(false);

if ($wholefile[$offset] == ':' || $wholefile[$offset] == 'e')

return array(0, ++$offset);

return array(false);

}

$ret[0] = 0;

for(;;) {

if ($wholefile[$offset] >= '0' && $wholefile[$offset] <= '9') {

$ret[0] *= 10;

//Added 2005.02.21 - VisiGod

//Changing the type of variable from integer to double to prevent a numeric overflow

settype($ret[0],'double');

//Added 2005.02.21 - VisiGod

$ret[0] += ord($wholefile[$offset]) - ord('0');

$offset++;

}else if ($wholefile[$offset] == 'e' || $wholefile[$offset] == ':') {

// Tolerate : or e because this is a multiuse function

$ret[1] = $offset+1;

if ($negative) {

if ($ret[0] == 0)

return array(false);

$ret[0] = - $ret[0];

}

return $ret;

} else return array(false);

}

}

function decodeEntry($wholefile, $offset=0) {

if ($wholefile[$offset] == 'd')

return $this->decodeDict($wholefile, $offset);

if ($wholefile[$offset] == 'l')

return $this->decodelist($wholefile, $offset);

if ($wholefile[$offset] == 'i')

return $this->numberdecode($wholefile, ++$offset);

// String value: decode number, then grab substring

$info = $this->numberdecode($wholefile, $offset);

if ($info[0] === false)

return array(false);

$ret[0] = substr($wholefile, $info[1], $info[0]);

$ret[1] = $info[1]+strlen($ret[0]);

return $ret;

}

function decodeList($wholefile, $offset) {

if ($wholefile[$offset] != 'l')

return array(false);

$offset++;

$ret = array();

for ($i=0;;$i++) {

if ($wholefile[$offset] == 'e')

break;

$value = $this->decodeEntry($wholefile, $offset);

if ($value[0] === false)

return array(false);

$ret[$i] = $value[0];

$offset = $value[1];

}

// The empty list is an empty array. Seems fine.

return array(0=>$ret, 1=>++$offset);

}

// Tries to construct an array

function decodeDict($wholefile, $offset=0) {

if ($wholefile[$offset] == 'l')

return $this->decodeList($wholefile, $offset);

if ($wholefile[$offset] != 'd')

return false;

$ret=array();

$offset++;

for (;;) {

if ($wholefile[$offset] == 'e'){

$offset++;

break;

}

$left = $this->decodeEntry($wholefile, $offset);

if (!$left[0])

return false;

$offset = $left[1];

if ($wholefile[$offset] == 'd') {

// Recurse

$value = $this->decodedict($wholefile, $offset);

if (!$value[0])

return false;

$ret[addslashes($left[0])] = $value[0];

$offset= $value[1];

continue;

}

if ($wholefile[$offset] == 'l') {

$value = $this->decodeList($wholefile, $offset);

if (!$value[0] && is_bool($value[0]))

return false;

$ret[addslashes($left[0])] = $value[0];

$offset = $value[1];

continue;

}

$value = $this->decodeEntry($wholefile, $offset);

if ($value[0] === false)

return false;

$ret[addslashes($left[0])] = $value[0];

$offset = $value[1];

}

return array(0=>(empty($ret)?true:$ret), 1=>$offset);

}

}

function BDecode($wholefile) {

$decoder = new BDecode;

$return = $decoder->decodeEntry($wholefile);

return $return[0];

}

?>

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值