From 142cc44dc96bf9f2c7fafdda4b5c2c42adb41e78 Mon Sep 17 00:00:00 2001 From: mykola2312 <49044616+mykola2312@users.noreply.github.com> Date: Fri, 18 Mar 2022 13:08:00 +0200 Subject: [PATCH] bdecode implement lists --- bencoder.php | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/bencoder.php b/bencoder.php index cf27645..6f8e426 100644 --- a/bencoder.php +++ b/bencoder.php @@ -43,21 +43,27 @@ function bencode($var) } } -function bdecode($enc) +function bdecode($enc, &$off = null) { $result = null; - $encLen = strlen($enc); - for ($i = 0; $i < $encLen; $i++) - { - $type = substr($enc, $i, 1); - if ($type == 'i') { - $start = ++$i; - $end = strpos($enc, 'e', $i); - $result = substr($enc, $start, $end - $start); - $i = $end; - continue; + if ($off == null) { + $_off = 0; + $off = $_off; + } + + $type = substr($enc, $off, 1); + if ($type == 'i') { + $end = strpos($enc, 'e', $off + 1); + $result = intval(substr($enc, $off + 1, $end - $off - 1)); + $off = $end + 1; + } else if ($type == 'l') { + $off++; + $result = array(); + while (substr($enc, $off, 1) != 'e') { + array_push($result, bdecode($enc, $off)); } } + return $result; }