bencode 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/bencode.rb +51 -28
- data/test/tc_bdecode.rb +1 -0
- data/test/tc_bencode.rb +2 -1
- metadata +7 -6
data/lib/bencode.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
#
|
2
|
-
#
|
3
|
-
|
4
|
-
# bencode is a Ruby implementation of the Bencode data serialization
|
1
|
+
# = bencode.rb - Bencode Library
|
2
|
+
#
|
3
|
+
# Bencode is a Ruby implementation of the Bencode data serialization
|
5
4
|
# format used in the BitTorrent protocol.
|
6
5
|
#
|
7
6
|
# == Synopsis
|
@@ -10,6 +9,45 @@
|
|
10
9
|
# 42.bencode #=> "i42e"
|
11
10
|
# [1, 2, 3].bencode #=> "li1ei2ei3ee"
|
12
11
|
#
|
12
|
+
# Bencoding (pronounced _bee-encode_) is a simple protocol, consiting of
|
13
|
+
# only 4 value types.
|
14
|
+
#
|
15
|
+
# === Integers ===
|
16
|
+
#
|
17
|
+
# An integer is encoded an _i_ followed by the numeral itself, followed
|
18
|
+
# by an _e_. Leading zeros are not allowed. Negative values are prefixed
|
19
|
+
# with a minus sign.
|
20
|
+
#
|
21
|
+
# 42.bencode #=> "i42e"
|
22
|
+
# -2.bencode #=> "i-2e"
|
23
|
+
# 0.bencode #=> "i0e"
|
24
|
+
#
|
25
|
+
# === Strings ===
|
26
|
+
#
|
27
|
+
# Strings are a sequence of zero or more bytes. It is encoded as
|
28
|
+
# _<length>:<contents>_, where _length_ is the lenth of _contents_. _length_
|
29
|
+
# must be non-negative.
|
30
|
+
#
|
31
|
+
# "".bencode #=> "0:"
|
32
|
+
# "foo".bencode #=> "3:foo"
|
33
|
+
#
|
34
|
+
# === Lists ===
|
35
|
+
#
|
36
|
+
# Lists are encoded as _l_ followed by the elements, followed by _e_.
|
37
|
+
# There is no element seperator.
|
38
|
+
#
|
39
|
+
# [1, 2, 3].bencode #=> "li1ei2ei3ee"
|
40
|
+
#
|
41
|
+
# === Dictionaries ===
|
42
|
+
#
|
43
|
+
# Dictionaries are encoded as _d<contents>e_, where _contents_ is a sequence
|
44
|
+
# of keys and values. Each value is immediately preceded by a key. Keys must
|
45
|
+
# be strings, and will appear in lexicographical order.
|
46
|
+
#
|
47
|
+
# {"foo" => 3, "bar" => 1, "baz" => 2}.bencode
|
48
|
+
# #=> "d3:bari1e3:bazi2e3:fooi3ee"
|
49
|
+
#
|
50
|
+
#
|
13
51
|
# == Authors
|
14
52
|
#
|
15
53
|
# * Daniel Schierbeck
|
@@ -29,6 +67,7 @@
|
|
29
67
|
# Bencode is distributed in the hope that it will be useful, but WITHOUT ANY
|
30
68
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
31
69
|
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
70
|
+
#
|
32
71
|
|
33
72
|
class Object
|
34
73
|
# Raises an exception. Subclasses of Object must themselves
|
@@ -133,28 +172,16 @@ class BencodeError < StandardError
|
|
133
172
|
end
|
134
173
|
end
|
135
174
|
|
136
|
-
class BdecodeError < StandardError
|
137
|
-
attr_reader :pos
|
138
|
-
|
139
|
-
def initialize(pos = nil) # :nodoc:
|
140
|
-
@pos = pos
|
141
|
-
end
|
142
|
-
|
143
|
-
def to_s # :nodoc:
|
144
|
-
if pos.nil?
|
145
|
-
"syntax error"
|
146
|
-
else
|
147
|
-
"syntax error near position #{pos}"
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
175
|
+
class BdecodeError < StandardError; end
|
151
176
|
|
152
177
|
module Bencode
|
153
178
|
class << self
|
179
|
+
# Bencodes +obj+
|
154
180
|
def dump(obj)
|
155
181
|
obj.bencode
|
156
182
|
end
|
157
183
|
|
184
|
+
# Bdecodes +str+
|
158
185
|
def load(str)
|
159
186
|
require 'strscan'
|
160
187
|
|
@@ -164,6 +191,7 @@ module Bencode
|
|
164
191
|
return obj
|
165
192
|
end
|
166
193
|
|
194
|
+
# Bdecodes the file located at +path+
|
167
195
|
def load_file(path)
|
168
196
|
load(File.open(path).read)
|
169
197
|
end
|
@@ -171,12 +199,10 @@ module Bencode
|
|
171
199
|
def parse(scanner) # :nodoc:
|
172
200
|
case token = scanner.scan(/[ild]|\d+:|\s/)
|
173
201
|
when nil
|
174
|
-
raise BdecodeError
|
202
|
+
raise BdecodeError
|
175
203
|
when "i"
|
176
204
|
number = scanner.scan(/0|(?:-?[1-9][0-9]*)/)
|
177
|
-
unless number and scanner.scan(/e/)
|
178
|
-
raise BdecodeError, scanner.pos
|
179
|
-
end
|
205
|
+
raise BdecodeError unless number and scanner.scan(/e/)
|
180
206
|
return number.to_i
|
181
207
|
when "l"
|
182
208
|
ary = []
|
@@ -189,10 +215,7 @@ module Bencode
|
|
189
215
|
hsh = {}
|
190
216
|
until scanner.peek(1) == "e"
|
191
217
|
key, value = parse(scanner), parse(scanner)
|
192
|
-
unless key.is_a? String
|
193
|
-
raise BdecodeError, "error at #{scanner.pos}: " +
|
194
|
-
"key must be a string"
|
195
|
-
end
|
218
|
+
raise BdecodeError, "key must be a string" unless key.is_a? String
|
196
219
|
hsh.store(key, value)
|
197
220
|
end
|
198
221
|
scanner.pos += 1
|
@@ -205,7 +228,7 @@ module Bencode
|
|
205
228
|
when /\s/
|
206
229
|
nil
|
207
230
|
else
|
208
|
-
raise BdecodeError
|
231
|
+
raise BdecodeError
|
209
232
|
end
|
210
233
|
end
|
211
234
|
|
data/test/tc_bdecode.rb
CHANGED
data/test/tc_bencode.rb
CHANGED
metadata
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.
|
2
|
+
rubygems_version: 0.9.0
|
3
3
|
specification_version: 1
|
4
4
|
name: bencode
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2006-09-
|
8
|
-
summary:
|
6
|
+
version: 0.3.0
|
7
|
+
date: 2006-09-29 00:00:00 +02:00
|
8
|
+
summary: A Ruby implementation of the Bencode encoding used by BitTorrent
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email:
|
@@ -25,15 +25,16 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
25
25
|
platform: ruby
|
26
26
|
signing_key:
|
27
27
|
cert_chain:
|
28
|
+
post_install_message:
|
28
29
|
authors:
|
29
30
|
- Daniel Schierbeck
|
30
31
|
files:
|
31
32
|
- lib/bencode.rb
|
32
|
-
- test/tc_bdecode.rb
|
33
33
|
- test/tc_bencode.rb
|
34
|
-
test_files:
|
35
34
|
- test/tc_bdecode.rb
|
35
|
+
test_files:
|
36
36
|
- test/tc_bencode.rb
|
37
|
+
- test/tc_bdecode.rb
|
37
38
|
rdoc_options: []
|
38
39
|
|
39
40
|
extra_rdoc_files: []
|