-
Notifications
You must be signed in to change notification settings - Fork 2
/
bcode.rb
172 lines (145 loc) · 2.6 KB
/
bcode.rb
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
class Bencode
def initialize
@ret = ""
end
public
def encode(x)
@ret = ""
encode_auto(x)
return @ret
end
private
def encode_auto(x)
case x.class.to_s
when "String"
encode_string(x)
when "Fixnum"
encode_int(x)
when "Hash"
encode_dict(x)
when "Array"
encode_list(x)
else
end
end
def encode_int(x)
@ret = @ret + "i" + x.to_s + "e"
end
def encode_bool(x)
if x then
encode_int(1)
else
encode_int(0)
end
end
def encode_string(x)
@ret = @ret + x.length.to_s + ":" + x
end
def encode_list(x)
@ret = @ret + "l"
x.each do |v|
encode_auto(v)
end
@ret = @ret + "e"
end
def encode_dict(x)
@ret = @ret + "d"
x.each do |k,v|
encode_string(k)
encode_auto(v)
end
@ret = @ret + "e"
end
end
class Bdecode
def decode(x)
ret = nil
begin
@pos = 0
@x = x
ret = decode_auto()
rescue
puts "error:#{$!} at:#{$@}"
ensure
return ret
end
end
def decode_auto()
if @x[@pos, 1] == "i" then
return decode_int()
elsif @x[@pos, 1] == "l" then
return decode_list()
elsif @x[@pos, 1] == "d" then
return decode_dict()
else
return decode_string()
end
end
def decode_int()
ret = 0
len = 0
if @x[@pos,1].eql?("i") then
@pos = @pos + 1
while not @x[@pos + len, 1].eql?("e") do
len = len + 1
end
ret = Integer(@x[@pos, len])
@pos = @pos + len + 1
end
return ret
end
def decode_string()
=begin
x = @x[@pos, @x.length - @pos]
len = x.split(":")[0]
=end
len = 0
while len < (@x.length - @pos) and not @x[@pos + len, 1] == ":" do
len = len + 1
end
if len == 0 then
@pos = @pos + 1
return ""
else
len = @x[@pos, len]
@pos = @pos + len.length + 1
len = Integer(len)
ret = @x[@pos, len]
@pos = @pos + len
return ret
end
end
def decode_list()
ret = Array.new
if @x[@pos, 1].eql?("l") then
@pos = @pos + 1
while not @x[@pos, 1].eql?("e") do
ret << decode_auto()
end
@pos = @pos + 1
end
return ret
end
def decode_dict()
ret = Hash.new
if @x[@pos, 1].eql?("d") then
@pos = @pos + 1
while not @x[@pos, 1].eql?("e") do
k = decode_string()
v = decode_auto()
ret[k] = v
end
end
@pos = @pos + 1
return ret
end
end
=begin
test = Bencode.new
x = {"t"=>"aa", "y"=>"e", "e"=>[201,"A Generic Error Ocurred"]}
ret = test.encode(x)
puts ret
test2 = Bdecode.new
ret = test2.decode(ret)
puts ret["e"]
=end