This seems to be fine. removed stripslashes. Back up your files before trying this. Let me know if its fine. Seems fine for me
BEncode
BDecode
BEncode
declare(strict_types=1);
class BEncode
{
// Sort dictionary keys
private function makeSorted(array $array): array
{
ksort($array);
return $array;
}
// Encode a single entry
private function encodeEntry($entry, string &$fd): void
{
if (is_int($entry) || is_float($entry)) {
$fd .= "i{$entry}e";
} elseif (is_string($entry)) {
$fd .= strlen($entry) . ":{$entry}";
} else {
throw new InvalidArgumentException("Unsupported entry type.");
}
}
// Encode a list
private function encodeList(array $list, string &$fd): void
{
$fd .= "l";
foreach ($list as $item) {
$this->decideEncode($item, $fd);
}
$fd .= "e";
}
// Encode a dictionary
private function encodeDict(array $dict, string &$fd): void
{
$fd .= "d";
$sortedDict = $this->makeSorted($dict);
foreach ($sortedDict as $key => $value) {
$this->encodeEntry($key, $fd);
$this->decideEncode($value, $fd);
}
$fd .= "e";
}
// Decide what to encode
private function decideEncode($data, string &$fd): void
{
if (is_array($data)) {
if (array_keys($data) === range(0, count($data) - 1)) {
$this->encodeList($data, $fd);
} else {
$this->encodeDict($data, $fd);
}
} else {
$this->encodeEntry($data, $fd);
}
}
// Public method to encode data
public function encode($data): string
{
$fd = "";
$this->decideEncode($data, $fd);
return $fd;
}
}
// Usage example:
function BEncode($data): string
{
return (new BEncode())->encode($data);
}
BDecode
declare(strict_types=1);
class BDecode
{
public function numberDecode(string $wholefile, int $start): array
{
$ret = [0];
$offset = $start;
$negative = false;
if ($wholefile[$offset] === '-') {
$negative = true;
$offset++;
}
if ($wholefile[$offset] === '0') {
$offset++;
if ($negative) {
return [false];
}
if ($wholefile[$offset] === ':' || $wholefile[$offset] === 'e') {
$ret[0] = 0;
$ret[1] = $offset + 1;
return $ret;
}
return [false];
}
while (isset($wholefile[$offset]) && ctype_digit($wholefile[$offset])) {
$ret[0] = $ret[0] * 10 + (int)$wholefile[$offset];
$offset++;
}
if ($wholefile[$offset] === 'e' || $wholefile[$offset] === ':') {
$ret[1] = $offset + 1;
if ($negative) {
if ($ret[0] === 0) {
return [false];
}
$ret[0] = -$ret[0];
}
return $ret;
}
return [false];
}
public function decodeEntry(string $wholefile, int $offset = 0): array
{
switch ($wholefile[$offset]) {
case 'd':
return $this->decodeDict($wholefile, $offset);
case 'l':
return $this->decodeList($wholefile, $offset);
case 'i':
return $this->numberDecode($wholefile, $offset + 1);
default:
$info = $this->numberDecode($wholefile, $offset);
if ($info[0] === false) {
return [false];
}
$ret[0] = substr($wholefile, $info[1], $info[0]);
$ret[1] = $info[1] + strlen($ret[0]);
return $ret;
}
}
public function decodeList(string $wholefile, int $start): array
{
if ($wholefile[$start] !== 'l') {
return [false];
}
$offset = $start + 1;
$ret = [];
while ($wholefile[$offset] !== 'e') {
$value = $this->decodeEntry($wholefile, $offset);
if ($value[0] === false) {
return [false];
}
$ret[] = $value[0];
$offset = $value[1];
}
return [$ret, $offset + 1];
}
public function decodeDict(string $wholefile, int $start = 0): array
{
if ($wholefile[$start] !== 'd') {
return [false];
}
$offset = $start + 1;
$ret = [];
while ($wholefile[$offset] !== 'e') {
$keyInfo = $this->decodeEntry($wholefile, $offset);
if ($keyInfo[0] === false) {
return [false];
}
$key = $keyInfo[0];
$offset = $keyInfo[1];
$valueInfo = $this->decodeEntry($wholefile, $offset);
if ($valueInfo[0] === false) {
return [false];
}
$ret[$key] = $valueInfo[0];
$offset = $valueInfo[1];
}
return [$ret, $offset + 1];
}
}
// Use this function, e.g., BDecode("d8:announce44:http://www...");
function BDecode(string $wholefile)
{
$decoder = new BDecode();
$result = $decoder->decodeEntry($wholefile);
return $result[0];
}
TTv3 Micro Edition
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Ubuntu 20.04.6 LTS Server
Mysql Version: 10.3.39-MariaDB-0ubuntu0.20.04.2
PHP Version: 7.4.3-4ubuntu2.20
Apache Version: Apache/2.4.59 (Ubuntu)
Last edited by MicroMonkey on 2024-12-25 20:48:08
