-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBencode.php
More file actions
191 lines (176 loc) · 5.14 KB
/
Bencode.php
File metadata and controls
191 lines (176 loc) · 5.14 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
<?php
/**
* Contains a pair of recursive functions that implement the bencode data
* encoding format.
*
* @author Michael J. I. Jackson <mjijackson@gmail.com>
*/
class Bencode
{
/**
* The version of the library.
*
* @var string
*/
const VERSION = "1.0";
/**
* Bencodes the given data structure.
*
* @param mixed
* @return string
* @throws Exception
*/
public static function encode($value)
{
if (is_null($value)) {
return "0:";
}
if (is_int($value)) {
return "i" . $value . "e";
}
if (is_string($value)) {
return strlen($value) . ":" . $value;
}
if (is_array($value)) {
if (self::isAssoc($value)) {
ksort($value, SORT_STRING);
$buffer = "d";
foreach ($value as $key => $v) {
$buffer .= self::encode(strval($key));
$buffer .= self::encode($v);
}
$buffer .= "e";
} else {
ksort($value, SORT_NUMERIC);
$buffer = "l";
foreach ($value as $v) {
$buffer .= self::encode($v);
}
$buffer .= "e";
}
return $buffer;
}
throw new Exception("Unable to encode data type: " . gettype($value));
}
/**
* Decodes the given string. The second parameter is only used in recursion.
*
* @param string
* @param int
* @return mixed
* @throws Exception
*/
public static function decode($tokens, &$i=0)
{
if (is_string($tokens)) {
$tokens = str_split($tokens);
}
switch ($tokens[$i]) {
case "d":
$dict = array();
while (isset($tokens[++$i])) {
if ($tokens[$i] == "e") {
return $dict;
} else {
$key = self::decode($tokens, $i);
if (isset($tokens[++$i])) {
$dict[$key] = self::decode($tokens, $i);
} else {
throw new Exception("Dictionary key ($key) without a value at index $i");
}
}
}
throw new Exception("Unterminated dictionary at index $i");
case "l":
$list = array();
while (isset($tokens[++$i])) {
if ($tokens[$i] == "e") {
return $list;
} else {
$list[] = self::decode($tokens, $i);
}
}
throw new Exception("Unterminated list at index $i");
case "i":
$buffer = '';
while (isset($tokens[++$i])) {
if ($tokens[$i] == "e") {
return intval($buffer);
} elseif (ctype_digit($tokens[$i])) {
$buffer .= $tokens[$i];
} else {
throw new Exception("Unexpected token while parsing integer at index $i: {$tokens[$i]}");
}
}
throw new Exception("Unterminated integer at index $i");
case ctype_digit($tokens[$i]):
$length = $tokens[$i];
while (isset($tokens[++$i])) {
if ($tokens[$i] == ":") {
break;
} elseif (ctype_digit($tokens[$i])) {
$length .= $tokens[$i];
} else {
throw new Exception("Unexpected token while parsing string length at index $i: {$tokens[$i]}");
}
}
$end = $i + intval($length);
$buffer = '';
while (isset($tokens[++$i])) {
if ($i <= $end) {
$buffer .= $tokens[$i];
if ($i == $end) {
return $buffer;
}
}
}
throw new Exception("Unterminated string at index $i");
}
throw new Exception("Unexpected token at index $i: {$tokens[$i]}");
}
/**
* Tells whether an array is associative or not. In order to be non-associative,
* each of the array's key numbers must correspond exactly to it's position
* in the array.
*
* @param array
* @return bool
*/
public static function isAssoc($array)
{
return count($array) !== array_reduce(array_keys($array), array("Bencode", "isAssocCallback"), 0);
}
/**
* A callback function used by {@link isAssoc()}.
*
* @return int
*/
protected static function isAssocCallback($a, $b)
{
return $a === $b ? $a + 1 : 0;
}
}
/**
* Shorthand for {@link Bencode::encode()}.
*
* @param mixed
* @return string
* @throws Exception
* @see Bencode::encode()
*/
function bencode($value)
{
return Bencode::encode($value);
}
/**
* Shorthand for {@link Bencode::decode()}.
*
* @param string
* @return mixed
* @throws Exception
* @see Bencode::decode()
*/
function bdecode($value)
{
return Bencode::decode($value);
}