|
@@ -1,196 +1,187 @@
|
1
|
|
-<?php
|
|
1
|
+<?
|
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
|
|
-{
|
8
|
|
- private $Data;
|
9
|
|
- private $Length;
|
10
|
|
- private $Pos = 0;
|
11
|
|
- public $Dec = [];
|
12
|
|
- public $ExitOnError = true;
|
13
|
|
- const SnipLength = 40;
|
|
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;
|
14
|
13
|
|
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();
|
|
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;
|
38
|
24
|
}
|
39
|
|
-
|
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();
|
|
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;
|
55
|
34
|
}
|
|
35
|
+ return $this->decode();
|
|
36
|
+ }
|
56
|
37
|
|
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;
|
|
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;
|
81
|
47
|
}
|
|
48
|
+ if (!$this->Data = @file_get_contents($Path, FILE_BINARY)) {
|
|
49
|
+ return $this->error("Error: file '$Path' could not be opened.\n");
|
|
50
|
+ }
|
|
51
|
+ return $this->decode();
|
|
52
|
+ }
|
82
|
53
|
|
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]) {
|
91
|
|
- case 'i':
|
92
|
|
- $this->Pos++;
|
93
|
|
- $Value = substr($this->Data, $this->Pos, strpos($this->Data, 'e', $this->Pos) - $this->Pos);
|
94
|
|
- if (!ctype_digit($Value) && !($Value[0] == '-' && ctype_digit(substr($Value, 1)))) {
|
95
|
|
- return $this->error();
|
96
|
|
- }
|
97
|
|
- $this->Pos += strlen($Value) + 1;
|
98
|
|
- return Int64::make($Value);
|
99
|
|
-
|
100
|
|
- case 'l':
|
101
|
|
- $Value = [];
|
102
|
|
- $this->Pos++;
|
103
|
|
- while ($this->Data[$this->Pos] != 'e') {
|
104
|
|
- if ($this->Pos >= $this->Length) {
|
105
|
|
- return $this->error();
|
106
|
|
- }
|
107
|
|
- $Value[] = $this->_bdec();
|
108
|
|
- }
|
109
|
|
- $this->Pos++;
|
110
|
|
- return $Value;
|
|
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();
|
|
75
|
+ }
|
|
76
|
+ return $this->Dec;
|
|
77
|
+ }
|
111
|
78
|
|
112
|
|
- case 'd':
|
113
|
|
- $Value = [];
|
114
|
|
- $this->Pos++;
|
115
|
|
- while ($this->Data[$this->Pos] != 'e') {
|
116
|
|
- $Length = substr($this->Data, $this->Pos, strpos($this->Data, ':', $this->Pos) - $this->Pos);
|
117
|
|
- if (!ctype_digit($Length)) {
|
118
|
|
- return $this->error();
|
119
|
|
- }
|
120
|
|
- $this->Pos += strlen($Length) + $Length + 1;
|
121
|
|
- $Key = substr($this->Data, $this->Pos - $Length, $Length);
|
122
|
|
- if ($this->Pos >= $this->Length) {
|
123
|
|
- return $this->error();
|
124
|
|
- }
|
125
|
|
- $Value[$Key] = $this->_bdec();
|
126
|
|
- }
|
127
|
|
- $this->Pos++;
|
128
|
|
- // Use boolean true to keep track of empty dictionaries
|
129
|
|
- return empty($Value) ? true : $Value;
|
|
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]) {
|
130
|
86
|
|
131
|
|
- default:
|
132
|
|
- $Length = substr($this->Data, $this->Pos, strpos($this->Data, ':', $this->Pos) - $this->Pos);
|
133
|
|
- if (!ctype_digit($Length)) {
|
134
|
|
- return $this->error(); // Even if the string is likely to be decoded correctly without this check, it's malformed
|
135
|
|
- }
|
136
|
|
- $this->Pos += strlen($Length) + $Length + 1;
|
137
|
|
- return substr($this->Data, $this->Pos - $Length, $Length);
|
|
87
|
+ case 'i':
|
|
88
|
+ $this->Pos++;
|
|
89
|
+ $Value = substr($this->Data, $this->Pos, strpos($this->Data, 'e', $this->Pos) - $this->Pos);
|
|
90
|
+ if (!ctype_digit($Value) && !($Value[0] == '-' && ctype_digit(substr($Value, 1)))) {
|
|
91
|
+ return $this->error();
|
138
|
92
|
}
|
139
|
|
- }
|
|
93
|
+ $this->Pos += strlen($Value) + 1;
|
|
94
|
+ return Int64::make($Value);
|
140
|
95
|
|
141
|
|
- /**
|
142
|
|
- * Convert everything to the correct data types and optionally escape strings
|
143
|
|
- *
|
144
|
|
- * @param bool $Escape whether to escape the textual data
|
145
|
|
- * @param mixed $Data decoded data or false to use the $Dec property
|
146
|
|
- * @return decoded data with more useful data types
|
147
|
|
- */
|
148
|
|
- public function dump($Escape = true, $Data = false)
|
149
|
|
- {
|
150
|
|
- if ($Data === false) {
|
151
|
|
- $Data = $this->Dec;
|
152
|
|
- }
|
153
|
|
- if (Int64::is_int($Data)) {
|
154
|
|
- return Int64::get($Data);
|
|
96
|
+ case 'l':
|
|
97
|
+ $Value = [];
|
|
98
|
+ $this->Pos++;
|
|
99
|
+ while ($this->Data[$this->Pos] != 'e') {
|
|
100
|
+ if ($this->Pos >= $this->Length) {
|
|
101
|
+ return $this->error();
|
|
102
|
+ }
|
|
103
|
+ $Value[] = $this->_bdec();
|
155
|
104
|
}
|
156
|
|
- if (is_bool($Data)) {
|
157
|
|
- return [];
|
|
105
|
+ $this->Pos++;
|
|
106
|
+ return $Value;
|
|
107
|
+
|
|
108
|
+ case 'd':
|
|
109
|
+ $Value = [];
|
|
110
|
+ $this->Pos++;
|
|
111
|
+ 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();
|
158
|
122
|
}
|
159
|
|
- if (is_array($Data)) {
|
160
|
|
- $Output = [];
|
161
|
|
- foreach ($Data as $Key => $Val) {
|
162
|
|
- $Output[$Key] = $this->dump($Escape, $Val);
|
163
|
|
- }
|
164
|
|
- return $Output;
|
|
123
|
+ $this->Pos++;
|
|
124
|
+ // Use boolean true to keep track of empty dictionaries
|
|
125
|
+ return empty($Value) ? true : $Value;
|
|
126
|
+
|
|
127
|
+ default:
|
|
128
|
+ $Length = substr($this->Data, $this->Pos, strpos($this->Data, ':', $this->Pos) - $this->Pos);
|
|
129
|
+ if (!ctype_digit($Length)) {
|
|
130
|
+ return $this->error(); // Even if the string is likely to be decoded correctly without this check, it's malformed
|
165
|
131
|
}
|
166
|
|
- return $Escape ? htmlentities($Data) : $Data;
|
|
132
|
+ $this->Pos += strlen($Length) + $Length + 1;
|
|
133
|
+ return substr($this->Data, $this->Pos - $Length, $Length);
|
167
|
134
|
}
|
|
135
|
+ }
|
168
|
136
|
|
169
|
|
- /**
|
170
|
|
- * Display an error and halt the operation unless the $ExitOnError property is false
|
171
|
|
- *
|
172
|
|
- * @param string $ErrMsg the error message to display
|
173
|
|
- */
|
174
|
|
- private function error($ErrMsg = false)
|
175
|
|
- {
|
176
|
|
- static $ErrorPos;
|
177
|
|
- if ($this->Pos === $ErrorPos) {
|
178
|
|
- // The recursive nature of the class requires this to avoid duplicate error messages
|
179
|
|
- return false;
|
180
|
|
- }
|
181
|
|
- if ($this->ExitOnError) {
|
182
|
|
- if ($ErrMsg === false) {
|
183
|
|
- printf(
|
184
|
|
- "Malformed string. Invalid character at pos 0x%X: %s\n",
|
185
|
|
- $this->Pos,
|
186
|
|
- str_replace(array("\r","\n"), array('',' '), htmlentities(substr($this->Data, $this->Pos, self::SnipLength)))
|
187
|
|
- );
|
188
|
|
- } else {
|
189
|
|
- echo $ErrMsg;
|
190
|
|
- }
|
191
|
|
- exit();
|
192
|
|
- }
|
193
|
|
- $ErrorPos = $this->Pos;
|
194
|
|
- return false;
|
|
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
|
+ }
|
|
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
|
+
|
|
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;
|
|
174
|
+ }
|
|
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();
|
195
|
183
|
}
|
|
184
|
+ $ErrorPos = $this->Pos;
|
|
185
|
+ return false;
|
|
186
|
+ }
|
196
|
187
|
}
|