forked from ahmyi/rivettracker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BEncode.php
122 lines (105 loc) · 2.26 KB
/
BEncode.php
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
<?php
// Woohoo! Who needs mhash or PHP 4.3?
// Don't require it. Still recommended, but not mandatory.
if (!function_exists("sha1"))
@include_once("sha1lib.php");
// We'll protect the namespace of our code
// using a class
class BEncode
{
// Dictionary keys must be sorted. foreach tends to iterate over the order
// the array was made, so we make a new one in sorted order. :)
/*
function makeSorted($array)
{
$i = 0;
// Shouldn't happen!
if (empty($array))
return $array;
foreach($array as $key => $value)
$keys[$i++] = stripslashes($key);
sort($keys);
for ($i=0 ; isset($keys[$i]); $i++)
$return[addslashes($keys[$i])] = $array[addslashes($keys[$i])];
return $return;
}
*/
// Encodes strings, integers and empty dictionaries.
// $unstrip is set to true when decoding dictionary keys
function encodeEntry($entry, &$fd, $unstrip = false)
{
if (is_bool($entry))
{
$fd .= "de";
return;
}
if (is_int($entry) || is_float($entry))
{
$fd .= "i".$entry."e";
return;
}
if ($unstrip)
$myentry = stripslashes($entry);
else
$myentry = $entry;
$length = strlen($myentry);
$fd .= $length.":".$myentry;
return;
}
// Encodes lists
function encodeList($array, &$fd)
{
$fd .= "l";
// The empty list is defined as array();
if (empty($array))
{
$fd .= "e";
return;
}
for ($i = 0; isset($array[$i]); $i++)
$this->decideEncode($array[$i], $fd);
$fd .= "e";
}
// Passes lists and dictionaries accordingly, and has encodeEntry handle
// the strings and integers.
function decideEncode($unknown, &$fd)
{
if (is_array($unknown))
{
if (isset($unknown[0]) || empty($unknown))
return $this->encodeList($unknown, $fd);
else
return $this->encodeDict($unknown, $fd);
}
$this->encodeEntry($unknown, $fd);
}
// Encodes dictionaries
function encodeDict($array, &$fd)
{
$fd .= "d";
if (is_bool($array))
{
$fd .= "e";
return;
}
// NEED TO SORT!
//$newarray = $this->makeSorted($array);
ksort($array, SORT_STRING);
foreach($array as $left => $right)
{
$this->encodeEntry($left, $fd, true);
$this->decideEncode($right, $fd);
}
$fd .= "e";
return;
}
} // End of class declaration.
// Use this function in your own code.
function BEncode($array)
{
$string = "";
$encoder = new BEncode;
$encoder->decideEncode($array, $string);
return $string;
}
?>