bdecode implement lists
This commit is contained in:
parent
31aa07530a
commit
142cc44dc9
1 changed files with 17 additions and 11 deletions
26
bencoder.php
26
bencoder.php
|
|
@ -43,21 +43,27 @@ function bencode($var)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function bdecode($enc)
|
function bdecode($enc, &$off = null)
|
||||||
{
|
{
|
||||||
$result = null;
|
$result = null;
|
||||||
$encLen = strlen($enc);
|
if ($off == null) {
|
||||||
for ($i = 0; $i < $encLen; $i++)
|
$_off = 0;
|
||||||
{
|
$off = $_off;
|
||||||
$type = substr($enc, $i, 1);
|
}
|
||||||
|
|
||||||
|
$type = substr($enc, $off, 1);
|
||||||
if ($type == 'i') {
|
if ($type == 'i') {
|
||||||
$start = ++$i;
|
$end = strpos($enc, 'e', $off + 1);
|
||||||
$end = strpos($enc, 'e', $i);
|
$result = intval(substr($enc, $off + 1, $end - $off - 1));
|
||||||
$result = substr($enc, $start, $end - $start);
|
$off = $end + 1;
|
||||||
$i = $end;
|
} else if ($type == 'l') {
|
||||||
continue;
|
$off++;
|
||||||
|
$result = array();
|
||||||
|
while (substr($enc, $off, 1) != 'e') {
|
||||||
|
array_push($result, bdecode($enc, $off));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue