1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\Bencode |
5
|
|
|
* @copyright Copyright (c) The s9e authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\Bencode; |
9
|
|
|
|
10
|
|
|
use ArrayObject; |
11
|
|
|
use const SORT_STRING, false, true; |
12
|
|
|
use function preg_match, strcmp, strlen; |
13
|
|
|
|
14
|
|
|
class NonCompliantDecoder extends Decoder |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var bool Whether current dictionary needs to be sorted |
18
|
|
|
*/ |
19
|
|
|
protected bool $sortDictionary = false; |
20
|
|
|
|
21
|
5 |
|
protected function complianceError(string $message, int $offset): void |
22
|
|
|
{ |
23
|
|
|
// Do nothing |
24
|
5 |
|
} |
25
|
|
|
|
26
|
5 |
|
protected function decodeDictionary(): ArrayObject |
27
|
|
|
{ |
28
|
5 |
|
$previousState = $this->sortDictionary; |
29
|
5 |
|
$this->sortDictionary = false; |
30
|
5 |
|
$dictionary = parent::decodeDictionary(); |
31
|
5 |
|
if ($this->sortDictionary) |
32
|
|
|
{ |
33
|
4 |
|
$dictionary->ksort(SORT_STRING); |
34
|
|
|
} |
35
|
5 |
|
$this->sortDictionary = $previousState; |
36
|
|
|
|
37
|
5 |
|
return $dictionary; |
38
|
|
|
} |
39
|
|
|
|
40
|
4 |
|
protected function dictionaryComplianceError(string $key, string $lastKey): void |
41
|
|
|
{ |
42
|
4 |
|
$this->sortDictionary = true; |
43
|
|
|
} |
44
|
|
|
|
45
|
14 |
|
protected function readDigits(string $terminator): string |
46
|
|
|
{ |
47
|
14 |
|
if ($this->bencoded[$this->offset] === '0') |
48
|
|
|
{ |
49
|
|
|
// Skip past the trailing zeroes and stop at the next digit or the last zero |
50
|
10 |
|
preg_match('(0++[1-9]?)A', $this->bencoded, $m, 0, $this->offset); |
51
|
10 |
|
$this->offset += strlen($m[0]) - 1; |
52
|
|
|
} |
53
|
|
|
|
54
|
14 |
|
return parent::readDigits($terminator); |
55
|
|
|
} |
56
|
|
|
} |