|
|
@@ -1,94 +1,99 @@
|
|
1
|
|
-<?
|
|
|
1
|
+<?php
|
|
2
|
2
|
/**
|
|
3
|
3
|
* The decode class is simple and straightforward. The only thing to
|
|
4
|
4
|
* note is that empty dictionaries are represented by boolean trues
|
|
5
|
5
|
*/
|
|
6
|
|
-class BencodeDecode extends Bencode {
|
|
7
|
|
- private $Data;
|
|
8
|
|
- private $Length;
|
|
9
|
|
- private $Pos = 0;
|
|
10
|
|
- public $Dec = [];
|
|
11
|
|
- public $ExitOnError = true;
|
|
12
|
|
- const SnipLength = 40;
|
|
|
6
|
+class BencodeDecode extends Bencode
|
|
|
7
|
+{
|
|
|
8
|
+ private $Data;
|
|
|
9
|
+ private $Length;
|
|
|
10
|
+ private $Pos = 0;
|
|
|
11
|
+ public $Dec = [];
|
|
|
12
|
+ public $ExitOnError = true;
|
|
|
13
|
+ const SnipLength = 40;
|
|
13
|
14
|
|
|
14
|
|
- /**
|
|
15
|
|
- * Decode prepararations
|
|
16
|
|
- *
|
|
17
|
|
- * @param string $Arg bencoded string or path to bencoded file to decode
|
|
18
|
|
- * @param bool $IsPath needs to be true if $Arg is a path
|
|
19
|
|
- * @return decoded data with a suitable structure
|
|
20
|
|
- */
|
|
21
|
|
- function __construct($Arg = false, $IsPath = false, $Strict = true) {
|
|
22
|
|
- if (!$Strict) {
|
|
23
|
|
- $this->ExitOnError = false;
|
|
24
|
|
- }
|
|
25
|
|
- if ($Arg === false) {
|
|
26
|
|
- if (empty($this->Enc)) {
|
|
27
|
|
- return false;
|
|
28
|
|
- }
|
|
29
|
|
- } else {
|
|
30
|
|
- if ($IsPath === true) {
|
|
31
|
|
- return $this->bdec_file($Arg);
|
|
32
|
|
- }
|
|
33
|
|
- $this->Data = $Arg;
|
|
|
15
|
+ /**
|
|
|
16
|
+ * Decode prepararations
|
|
|
17
|
+ *
|
|
|
18
|
+ * @param string $Arg bencoded string or path to bencoded file to decode
|
|
|
19
|
+ * @param bool $IsPath needs to be true if $Arg is a path
|
|
|
20
|
+ * @return decoded data with a suitable structure
|
|
|
21
|
+ */
|
|
|
22
|
+ public function __construct($Arg = false, $IsPath = false, $Strict = true)
|
|
|
23
|
+ {
|
|
|
24
|
+ if (!$Strict) {
|
|
|
25
|
+ $this->ExitOnError = false;
|
|
|
26
|
+ }
|
|
|
27
|
+ if ($Arg === false) {
|
|
|
28
|
+ if (empty($this->Enc)) {
|
|
|
29
|
+ return false;
|
|
|
30
|
+ }
|
|
|
31
|
+ } else {
|
|
|
32
|
+ if ($IsPath === true) {
|
|
|
33
|
+ return $this->bdec_file($Arg);
|
|
|
34
|
+ }
|
|
|
35
|
+ $this->Data = $Arg;
|
|
|
36
|
+ }
|
|
|
37
|
+ return $this->decode();
|
|
34
|
38
|
}
|
|
35
|
|
- return $this->decode();
|
|
36
|
|
- }
|
|
37
|
39
|
|
|
38
|
|
- /**
|
|
39
|
|
- * Decodes a bencoded file
|
|
40
|
|
- *
|
|
41
|
|
- * @param $Path path to bencoded file to decode
|
|
42
|
|
- * @return decoded data with a suitable structure
|
|
43
|
|
- */
|
|
44
|
|
- public function bdec_file($Path = false) {
|
|
45
|
|
- if (empty($Path)) {
|
|
46
|
|
- return false;
|
|
47
|
|
- }
|
|
48
|
|
- if (!$this->Data = @file_get_contents($Path, FILE_BINARY)) {
|
|
49
|
|
- return $this->error("Error: file '$Path' could not be opened.\n");
|
|
|
40
|
+ /**
|
|
|
41
|
+ * Decodes a bencoded file
|
|
|
42
|
+ *
|
|
|
43
|
+ * @param $Path path to bencoded file to decode
|
|
|
44
|
+ * @return decoded data with a suitable structure
|
|
|
45
|
+ */
|
|
|
46
|
+ public function bdec_file($Path = false)
|
|
|
47
|
+ {
|
|
|
48
|
+ if (empty($Path)) {
|
|
|
49
|
+ return false;
|
|
|
50
|
+ }
|
|
|
51
|
+ if (!$this->Data = @file_get_contents($Path, FILE_BINARY)) {
|
|
|
52
|
+ return $this->error("Error: file '$Path' could not be opened.\n");
|
|
|
53
|
+ }
|
|
|
54
|
+ return $this->decode();
|
|
50
|
55
|
}
|
|
51
|
|
- return $this->decode();
|
|
52
|
|
- }
|
|
53
|
56
|
|
|
54
|
|
- /**
|
|
55
|
|
- * Decodes a string with bencoded data
|
|
56
|
|
- *
|
|
57
|
|
- * @param mixed $Arg bencoded data or false to decode the content of $this->Data
|
|
58
|
|
- * @return decoded data with a suitable structure
|
|
59
|
|
- */
|
|
60
|
|
- public function decode($Arg = false) {
|
|
61
|
|
- if ($Arg !== false) {
|
|
62
|
|
- $this->Data = $Arg;
|
|
63
|
|
- } elseif (!$this->Data) {
|
|
64
|
|
- $this->Data = $this->Enc;
|
|
65
|
|
- }
|
|
66
|
|
- if (!$this->Data) {
|
|
67
|
|
- return false;
|
|
68
|
|
- }
|
|
69
|
|
- $this->Length = strlen($this->Data);
|
|
70
|
|
- $this->Pos = 0;
|
|
71
|
|
- $this->Dec = $this->_bdec();
|
|
72
|
|
- if ($this->Pos < $this->Length) {
|
|
73
|
|
- // Not really necessary, but if the torrent is invalid, it's better to warn than to silently truncate it
|
|
74
|
|
- return $this->error();
|
|
|
57
|
+ /**
|
|
|
58
|
+ * Decodes a string with bencoded data
|
|
|
59
|
+ *
|
|
|
60
|
+ * @param mixed $Arg bencoded data or false to decode the content of $this->Data
|
|
|
61
|
+ * @return decoded data with a suitable structure
|
|
|
62
|
+ */
|
|
|
63
|
+ public function decode($Arg = false)
|
|
|
64
|
+ {
|
|
|
65
|
+ if ($Arg !== false) {
|
|
|
66
|
+ $this->Data = $Arg;
|
|
|
67
|
+ } elseif (!$this->Data) {
|
|
|
68
|
+ $this->Data = $this->Enc;
|
|
|
69
|
+ }
|
|
|
70
|
+ if (!$this->Data) {
|
|
|
71
|
+ return false;
|
|
|
72
|
+ }
|
|
|
73
|
+ $this->Length = strlen($this->Data);
|
|
|
74
|
+ $this->Pos = 0;
|
|
|
75
|
+ $this->Dec = $this->_bdec();
|
|
|
76
|
+ if ($this->Pos < $this->Length) {
|
|
|
77
|
+ // Not really necessary, but if the torrent is invalid, it's better to warn than to silently truncate it
|
|
|
78
|
+ return $this->error();
|
|
|
79
|
+ }
|
|
|
80
|
+ return $this->Dec;
|
|
75
|
81
|
}
|
|
76
|
|
- return $this->Dec;
|
|
77
|
|
- }
|
|
78
|
82
|
|
|
79
|
|
- /**
|
|
80
|
|
- * Internal decoding function that does the actual job
|
|
81
|
|
- *
|
|
82
|
|
- * @return decoded data with a suitable structure
|
|
83
|
|
- */
|
|
84
|
|
- private function _bdec() {
|
|
85
|
|
- switch ($this->Data[$this->Pos]) {
|
|
|
83
|
+ /**
|
|
|
84
|
+ * Internal decoding function that does the actual job
|
|
|
85
|
+ *
|
|
|
86
|
+ * @return decoded data with a suitable structure
|
|
|
87
|
+ */
|
|
|
88
|
+ private function _bdec()
|
|
|
89
|
+ {
|
|
|
90
|
+ switch ($this->Data[$this->Pos]) {
|
|
86
|
91
|
|
|
87
|
92
|
case 'i':
|
|
88
|
93
|
$this->Pos++;
|
|
89
|
94
|
$Value = substr($this->Data, $this->Pos, strpos($this->Data, 'e', $this->Pos) - $this->Pos);
|
|
90
|
95
|
if (!ctype_digit($Value) && !($Value[0] == '-' && ctype_digit(substr($Value, 1)))) {
|
|
91
|
|
- return $this->error();
|
|
|
96
|
+ return $this->error();
|
|
92
|
97
|
}
|
|
93
|
98
|
$this->Pos += strlen($Value) + 1;
|
|
94
|
99
|
return Int64::make($Value);
|
|
|
@@ -97,10 +102,10 @@ class BencodeDecode extends Bencode {
|
|
97
|
102
|
$Value = [];
|
|
98
|
103
|
$this->Pos++;
|
|
99
|
104
|
while ($this->Data[$this->Pos] != 'e') {
|
|
100
|
|
- if ($this->Pos >= $this->Length) {
|
|
101
|
|
- return $this->error();
|
|
102
|
|
- }
|
|
103
|
|
- $Value[] = $this->_bdec();
|
|
|
105
|
+ if ($this->Pos >= $this->Length) {
|
|
|
106
|
+ return $this->error();
|
|
|
107
|
+ }
|
|
|
108
|
+ $Value[] = $this->_bdec();
|
|
104
|
109
|
}
|
|
105
|
110
|
$this->Pos++;
|
|
106
|
111
|
return $Value;
|
|
|
@@ -109,16 +114,16 @@ class BencodeDecode extends Bencode {
|
|
109
|
114
|
$Value = [];
|
|
110
|
115
|
$this->Pos++;
|
|
111
|
116
|
while ($this->Data[$this->Pos] != 'e') {
|
|
112
|
|
- $Length = substr($this->Data, $this->Pos, strpos($this->Data, ':', $this->Pos) - $this->Pos);
|
|
113
|
|
- if (!ctype_digit($Length)) {
|
|
114
|
|
- return $this->error();
|
|
115
|
|
- }
|
|
116
|
|
- $this->Pos += strlen($Length) + $Length + 1;
|
|
117
|
|
- $Key = substr($this->Data, $this->Pos - $Length, $Length);
|
|
118
|
|
- if ($this->Pos >= $this->Length) {
|
|
119
|
|
- return $this->error();
|
|
120
|
|
- }
|
|
121
|
|
- $Value[$Key] = $this->_bdec();
|
|
|
117
|
+ $Length = substr($this->Data, $this->Pos, strpos($this->Data, ':', $this->Pos) - $this->Pos);
|
|
|
118
|
+ if (!ctype_digit($Length)) {
|
|
|
119
|
+ return $this->error();
|
|
|
120
|
+ }
|
|
|
121
|
+ $this->Pos += strlen($Length) + $Length + 1;
|
|
|
122
|
+ $Key = substr($this->Data, $this->Pos - $Length, $Length);
|
|
|
123
|
+ if ($this->Pos >= $this->Length) {
|
|
|
124
|
+ return $this->error();
|
|
|
125
|
+ }
|
|
|
126
|
+ $Value[$Key] = $this->_bdec();
|
|
122
|
127
|
}
|
|
123
|
128
|
$this->Pos++;
|
|
124
|
129
|
// Use boolean true to keep track of empty dictionaries
|
|
|
@@ -127,61 +132,66 @@ class BencodeDecode extends Bencode {
|
|
127
|
132
|
default:
|
|
128
|
133
|
$Length = substr($this->Data, $this->Pos, strpos($this->Data, ':', $this->Pos) - $this->Pos);
|
|
129
|
134
|
if (!ctype_digit($Length)) {
|
|
130
|
|
- return $this->error(); // Even if the string is likely to be decoded correctly without this check, it's malformed
|
|
|
135
|
+ return $this->error(); // Even if the string is likely to be decoded correctly without this check, it's malformed
|
|
131
|
136
|
}
|
|
132
|
137
|
$this->Pos += strlen($Length) + $Length + 1;
|
|
133
|
138
|
return substr($this->Data, $this->Pos - $Length, $Length);
|
|
134
|
139
|
}
|
|
135
|
|
- }
|
|
136
|
|
-
|
|
137
|
|
- /**
|
|
138
|
|
- * Convert everything to the correct data types and optionally escape strings
|
|
139
|
|
- *
|
|
140
|
|
- * @param bool $Escape whether to escape the textual data
|
|
141
|
|
- * @param mixed $Data decoded data or false to use the $Dec property
|
|
142
|
|
- * @return decoded data with more useful data types
|
|
143
|
|
- */
|
|
144
|
|
- public function dump($Escape = true, $Data = false) {
|
|
145
|
|
- if ($Data === false) {
|
|
146
|
|
- $Data = $this->Dec;
|
|
147
|
|
- }
|
|
148
|
|
- if (Int64::is_int($Data)) {
|
|
149
|
|
- return Int64::get($Data);
|
|
150
|
140
|
}
|
|
151
|
|
- if (is_bool($Data)) {
|
|
152
|
|
- return [];
|
|
153
|
|
- }
|
|
154
|
|
- if (is_array($Data)) {
|
|
155
|
|
- $Output = [];
|
|
156
|
|
- foreach ($Data as $Key => $Val) {
|
|
157
|
|
- $Output[$Key] = $this->dump($Escape, $Val);
|
|
158
|
|
- }
|
|
159
|
|
- return $Output;
|
|
160
|
|
- }
|
|
161
|
|
- return $Escape ? htmlentities($Data) : $Data;
|
|
162
|
|
- }
|
|
163
|
141
|
|
|
164
|
|
- /**
|
|
165
|
|
- * Display an error and halt the operation unless the $ExitOnError property is false
|
|
166
|
|
- *
|
|
167
|
|
- * @param string $ErrMsg the error message to display
|
|
168
|
|
- */
|
|
169
|
|
- private function error($ErrMsg = false) {
|
|
170
|
|
- static $ErrorPos;
|
|
171
|
|
- if ($this->Pos === $ErrorPos) {
|
|
172
|
|
- // The recursive nature of the class requires this to avoid duplicate error messages
|
|
173
|
|
- return false;
|
|
|
142
|
+ /**
|
|
|
143
|
+ * Convert everything to the correct data types and optionally escape strings
|
|
|
144
|
+ *
|
|
|
145
|
+ * @param bool $Escape whether to escape the textual data
|
|
|
146
|
+ * @param mixed $Data decoded data or false to use the $Dec property
|
|
|
147
|
+ * @return decoded data with more useful data types
|
|
|
148
|
+ */
|
|
|
149
|
+ public function dump($Escape = true, $Data = false)
|
|
|
150
|
+ {
|
|
|
151
|
+ if ($Data === false) {
|
|
|
152
|
+ $Data = $this->Dec;
|
|
|
153
|
+ }
|
|
|
154
|
+ if (Int64::is_int($Data)) {
|
|
|
155
|
+ return Int64::get($Data);
|
|
|
156
|
+ }
|
|
|
157
|
+ if (is_bool($Data)) {
|
|
|
158
|
+ return [];
|
|
|
159
|
+ }
|
|
|
160
|
+ if (is_array($Data)) {
|
|
|
161
|
+ $Output = [];
|
|
|
162
|
+ foreach ($Data as $Key => $Val) {
|
|
|
163
|
+ $Output[$Key] = $this->dump($Escape, $Val);
|
|
|
164
|
+ }
|
|
|
165
|
+ return $Output;
|
|
|
166
|
+ }
|
|
|
167
|
+ return $Escape ? htmlentities($Data) : $Data;
|
|
174
|
168
|
}
|
|
175
|
|
- if ($this->ExitOnError) {
|
|
176
|
|
- if ($ErrMsg === false) {
|
|
177
|
|
- printf("Malformed string. Invalid character at pos 0x%X: %s\n",
|
|
178
|
|
- $this->Pos, str_replace(array("\r","\n"), array('',' '), htmlentities(substr($this->Data, $this->Pos, self::SnipLength))));
|
|
179
|
|
- } else {
|
|
180
|
|
- echo $ErrMsg;
|
|
181
|
|
- }
|
|
182
|
|
- exit();
|
|
|
169
|
+
|
|
|
170
|
+ /**
|
|
|
171
|
+ * Display an error and halt the operation unless the $ExitOnError property is false
|
|
|
172
|
+ *
|
|
|
173
|
+ * @param string $ErrMsg the error message to display
|
|
|
174
|
+ */
|
|
|
175
|
+ private function error($ErrMsg = false)
|
|
|
176
|
+ {
|
|
|
177
|
+ static $ErrorPos;
|
|
|
178
|
+ if ($this->Pos === $ErrorPos) {
|
|
|
179
|
+ // The recursive nature of the class requires this to avoid duplicate error messages
|
|
|
180
|
+ return false;
|
|
|
181
|
+ }
|
|
|
182
|
+ if ($this->ExitOnError) {
|
|
|
183
|
+ if ($ErrMsg === false) {
|
|
|
184
|
+ printf(
|
|
|
185
|
+ "Malformed string. Invalid character at pos 0x%X: %s\n",
|
|
|
186
|
+ $this->Pos,
|
|
|
187
|
+ str_replace(array("\r","\n"), array('',' '), htmlentities(substr($this->Data, $this->Pos, self::SnipLength)))
|
|
|
188
|
+ );
|
|
|
189
|
+ } else {
|
|
|
190
|
+ echo $ErrMsg;
|
|
|
191
|
+ }
|
|
|
192
|
+ exit();
|
|
|
193
|
+ }
|
|
|
194
|
+ $ErrorPos = $this->Pos;
|
|
|
195
|
+ return false;
|
|
183
|
196
|
}
|
|
184
|
|
- $ErrorPos = $this->Pos;
|
|
185
|
|
- return false;
|
|
186
|
|
- }
|
|
187
|
197
|
}
|