From 2ed8ce0e4d30f2d832f9b35fefa64614ff0246e4 Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Tue, 9 Apr 2024 03:00:40 +0200 Subject: [PATCH 1/4] WiP --- src/Decoder.php | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Decoder.php b/src/Decoder.php index cceaf85..6d15bb6 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -146,23 +146,13 @@ protected function decodeDictionary(): ArrayObject ++$this->offset; while ($this->offset <= $this->max) { - $c = $this->bencoded[$this->offset]; - if ($c === 'e') + $key = $this->decodeDictionaryKey(); + if (!isset($key)) { ++$this->offset; return new ArrayObject($values, ArrayObject::ARRAY_AS_PROPS); } - - // Quickly match the most common keys found in dictionaries - $key = match ($c) - { - '4' => $this->decodeFastString('4:path', 6, 'path' ), - '6' => $this->decodeFastString('6:length', 8, 'length' ), - '8' => $this->decodeFastString('8:announce', 10, 'announce'), - '5' => $this->decodeFastString('5:files', 7, 'files' ), - default => $this->decodeString() - }; if (strcmp($lastKey, $key) >= 0 && !empty($values)) { $this->dictionaryComplianceError($key, $lastKey); @@ -178,6 +168,20 @@ protected function decodeDictionary(): ArrayObject throw new DecodingException('Premature end of data', $this->len - 1); } + final protected function decodeDictionaryKey(): ?string + { + // Quickly match the most common keys found in dictionaries + return match ($this->bencoded[$this->offset]) + { + '4' => $this->decodeFastString('4:path', 6, 'path' ), + '6' => $this->decodeFastString('6:length', 8, 'length' ), + '8' => $this->decodeFastString('8:announce', 10, 'announce'), + '5' => $this->decodeFastString('5:files', 7, 'files' ), + 'e' => null, + default => $this->decodeString() + }; + } + /** * @param string $match Bencoded string to match * @param int $len Length of the bencoded string From eef8c7875d7a81d1edf00b74fa0f65f17192723c Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Tue, 9 Apr 2024 03:02:24 +0200 Subject: [PATCH 2/4] WiP --- src/Decoder.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Decoder.php b/src/Decoder.php index 6d15bb6..af6d31d 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -146,7 +146,16 @@ protected function decodeDictionary(): ArrayObject ++$this->offset; while ($this->offset <= $this->max) { - $key = $this->decodeDictionaryKey(); + // Quickly match the most common keys found in dictionaries + $key = match ($this->bencoded[$this->offset]) + { + '4' => $this->decodeFastString('4:path', 6, 'path' ), + '6' => $this->decodeFastString('6:length', 8, 'length' ), + '8' => $this->decodeFastString('8:announce', 10, 'announce'), + '5' => $this->decodeFastString('5:files', 7, 'files' ), + 'e' => null, + default => $this->decodeString() + }; if (!isset($key)) { ++$this->offset; @@ -170,16 +179,6 @@ protected function decodeDictionary(): ArrayObject final protected function decodeDictionaryKey(): ?string { - // Quickly match the most common keys found in dictionaries - return match ($this->bencoded[$this->offset]) - { - '4' => $this->decodeFastString('4:path', 6, 'path' ), - '6' => $this->decodeFastString('6:length', 8, 'length' ), - '8' => $this->decodeFastString('8:announce', 10, 'announce'), - '5' => $this->decodeFastString('5:files', 7, 'files' ), - 'e' => null, - default => $this->decodeString() - }; } /** From b14baecd305590834a32e7382e340d8434c14d3b Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Tue, 9 Apr 2024 03:10:53 +0200 Subject: [PATCH 3/4] WiP --- src/Decoder.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Decoder.php b/src/Decoder.php index af6d31d..eb1e155 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -162,7 +162,7 @@ protected function decodeDictionary(): ArrayObject return new ArrayObject($values, ArrayObject::ARRAY_AS_PROPS); } - if (strcmp($lastKey, $key) >= 0 && !empty($values)) + if (strcmp($lastKey, $key) >= 0) { $this->dictionaryComplianceError($key, $lastKey); } @@ -177,10 +177,6 @@ protected function decodeDictionary(): ArrayObject throw new DecodingException('Premature end of data', $this->len - 1); } - final protected function decodeDictionaryKey(): ?string - { - } - /** * @param string $match Bencoded string to match * @param int $len Length of the bencoded string From 453f66229172d3a8be6886c65138e99a8f7f4a82 Mon Sep 17 00:00:00 2001 From: JoshyPHP Date: Tue, 9 Apr 2024 03:20:08 +0200 Subject: [PATCH 4/4] WiP --- src/Decoder.php | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Decoder.php b/src/Decoder.php index eb1e155..6d15bb6 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -146,23 +146,14 @@ protected function decodeDictionary(): ArrayObject ++$this->offset; while ($this->offset <= $this->max) { - // Quickly match the most common keys found in dictionaries - $key = match ($this->bencoded[$this->offset]) - { - '4' => $this->decodeFastString('4:path', 6, 'path' ), - '6' => $this->decodeFastString('6:length', 8, 'length' ), - '8' => $this->decodeFastString('8:announce', 10, 'announce'), - '5' => $this->decodeFastString('5:files', 7, 'files' ), - 'e' => null, - default => $this->decodeString() - }; + $key = $this->decodeDictionaryKey(); if (!isset($key)) { ++$this->offset; return new ArrayObject($values, ArrayObject::ARRAY_AS_PROPS); } - if (strcmp($lastKey, $key) >= 0) + if (strcmp($lastKey, $key) >= 0 && !empty($values)) { $this->dictionaryComplianceError($key, $lastKey); } @@ -177,6 +168,20 @@ protected function decodeDictionary(): ArrayObject throw new DecodingException('Premature end of data', $this->len - 1); } + final protected function decodeDictionaryKey(): ?string + { + // Quickly match the most common keys found in dictionaries + return match ($this->bencoded[$this->offset]) + { + '4' => $this->decodeFastString('4:path', 6, 'path' ), + '6' => $this->decodeFastString('6:length', 8, 'length' ), + '8' => $this->decodeFastString('8:announce', 10, 'announce'), + '5' => $this->decodeFastString('5:files', 7, 'files' ), + 'e' => null, + default => $this->decodeString() + }; + } + /** * @param string $match Bencoded string to match * @param int $len Length of the bencoded string