-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclass.bdecode.php
More file actions
228 lines (201 loc) · 8.9 KB
/
class.bdecode.php
File metadata and controls
228 lines (201 loc) · 8.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/*********************************************************************
* Author: Andris Causs
* Email: cypher[at]inbox[dot]lv
* Date: July 07, 2006
* Purpose: Parse binary encoded (torrent) files into nested array.
*
* You are free to use and modify the code. Just do not copy it and post
* somewhere as your own.
* Therefore I reserve all rights to this code in modified or unmodified form.
*
*
* Class form:
* BDecode(string filepath);
*
* Basic usage:
* -- Require this file:
* require_once 'class.bdecode.php';
*
* -- Initialize new class instance like this:
* $torrent = new BDecode('C:\\path\\to\file.torrent');
* -- if your web server is running on Windows
* -- or
* $torrent = new BDecode('/path/to/file.torrent');
*
* -- You can access the resulting value this way:
* $torrent->result['my_value_name']
*
* -- Here is a list of some of the most used properties:
* $torrent->result['announce'] // string
* $torrent->result['announce-list'] // array
* $torrent->result['comment'] // string
* $torrent->result['created by'] // string
* $torrent->result['creation date'] // unix timestamp
* $torrent->result['encoding'] // string
* $torrent->result['info']['files'] // array
* $torrent->result['info']['files'][?]['length'] // integer
* $torrent->result['info']['files'][?]['path'] // string
* $torrent->result['info']['name'] // string
* $torrent->result['info']['piece length'] // integer
* $torrent->result['info']['pieces'] // string
* $torrent->result['info']['private'] // integer
* $torrent->result['modified-by'] // array
*
* See http://wiki.theory.org/BitTorrentSpecification for bittorrent specification
*/
final class BDecode {
private $content; // string containing contents of file
private $pointer = 0; // current position pointer in content
public $result = array(); // result array containing all decoded elements
/**************************************************************************
* Info: Parses bencoded file into array.
* Args: {string} filepath: full or relative path to bencoded file
**************************************************************************/
function __construct($filepath) {
$this->content = @file_get_contents($filepath);
if (!$this->content) {
$this->throwException('File does not exist!');
} else {
if (!isset($this->content)) {
$this->throwException('Error opening file!');
} else {
$this->result = $this->processElement();
}
}
unset($this->content);
}
/**************************************************************************
* Info: Clear class variables.
* Args: none
**************************************************************************/
function __destruct() {
unset($this->content);
unset($this->result);
}
/**************************************************************************
* Info: Terminates decoding process and returns error.
* Args: {string} error [optional] - error description
**************************************************************************/
private function throwException($error = 'error parsing file') {
$this->result = array();
$this->result['error'] = $error;
}
/**************************************************************************
* Info: Processes element depending on its type.
* Results in error if no valid identifier is found.
* Args: none
**************************************************************************/
private function processElement() {
switch($this->content[$this->pointer]) {
case 'd':
return $this->processDictionary();
break;
case 'l':
return $this->processList();
break;
case 'i':
return $this->processInteger();
break;
default:
if (is_numeric($this->content[$this->pointer])) {
return $this->processString();
} else {
$this->throwException('Unknown BEncode element');
}
break;
}
}
/**************************************************************************
* Info: Processes dictionary entries.
* Returns array of dictionary entries.
* Args: none
**************************************************************************/
private function processDictionary() {
if (!$this->isOfType('d'))
$this->throwException();
$res = array();
$this->pointer++;
while (!$this->isOfType('e')) {
$elemkey = $this->processString();
switch($this->content[$this->pointer]) {
case 'd':
$res[$elemkey] = $this->processDictionary();
break;
case 'l':
$res[$elemkey] = $this->processList();
break;
case 'i':
$res[$elemkey] = $this->processInteger();
break;
default:
if (is_numeric($this->content[$this->pointer])) {
$res[$elemkey] = $this->processString();
} else {
$this->throwException('Unknown BEncode element!');
}
break;
}
}
$this->pointer++;
return $res;
}
/**************************************************************************
* Info: Processes list entries.
* Returns array of list entries found between 'l' and 'e' identifiers.
* Args: none
**************************************************************************/
private function processList() {
if (!$this->isOfType('l'))
$this->throwException();
$res = array();
$this->pointer++;
while (!$this->isOfType('e'))
$res[] = $this->processElement();
$this->pointer++;
return $res;
}
/**************************************************************************
* Info: Processes integer value.
* Returns integer value found between 'i' and 'e' identifiers.
* Args: none
**************************************************************************/
private function processInteger() {
if (!$this->isOfType('e'))
$this->throwException();
$this->pointer++;
$delim_pos = strpos($this->content, 'e', $this->pointer);
$integer = substr($this->content, $this->pointer, $delim_pos - $this->pointer);
if (($integer == '-0') || ((substr($integer, 0, 1) == '0') && (strlen($integer) > 1)))
$this->throwException();
$integer = abs(intval($integer));
$this->pointer = $delim_pos + 1;
return $integer;
}
/**************************************************************************
* Info: Processes string value.
* Returns string value found after '%:' identifier, where '%' is any
* valid integer.
* Args: none
**************************************************************************/
private function processString() {
if (!is_numeric($this->content[$this->pointer])) {
$this->throwException();
}
$delim_pos = strpos($this->content, ':', $this->pointer);
$elem_len = intval(substr($this->content, $this->pointer, $delim_pos - $this->pointer));
$this->pointer = $delim_pos + 1;
$elem_name = substr($this->content, $this->pointer, $elem_len);
$this->pointer += $elem_len;
return $elem_name;
}
/**************************************************************************
* Info: Checks if identifier at current pointer is of supplied type.
* Args: {char} type - character denoting required type.
* Usually one of [d,l,i,e].
**************************************************************************/
private function isOfType($type) {
return ($this->content[$this->pointer] == $type);
}
}
?>