bencodr 2.0.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -1
- data/Rakefile +2 -0
- data/lib/bencodr/ext.rb +2 -0
- data/lib/bencodr/io.rb +3 -1
- data/lib/bencodr/object.rb +4 -1
- data/lib/bencodr/parser.rb +4 -3
- data/lib/bencodr/version.rb +1 -1
- data/spec/bencode_spec.rb +2 -2
- data/spec/bencodr/io_spec.rb +1 -0
- data/spec/samples/bencodr.torrent +1 -0
- data/spec/samples/mini.bencode +1 -1
- data/spec/shared_examples.rb +3 -0
- data/spec/spec_helper.rb +1 -0
- metadata +3 -1
data/README.md
CHANGED
@@ -8,7 +8,7 @@
|
|
8
8
|
## Synopsis
|
9
9
|
This gem provides a way to encode and parse bencodings used by the Bit Torrent protocol.
|
10
10
|
|
11
|
-
_Note: If using ruby 1.
|
11
|
+
_Note: If using ruby 1.8.x, use bencodr version 2.1.0. 3.0.0 is 1.9.x only, because it uses the 1.9.x encoding features._
|
12
12
|
|
13
13
|
## Installation
|
14
14
|
|
@@ -67,6 +67,7 @@ This will extend:
|
|
67
67
|
* Hash
|
68
68
|
* BEncodr::IO
|
69
69
|
* IO
|
70
|
+
* File
|
70
71
|
|
71
72
|
### String
|
72
73
|
BEncoded strings are length-prefixed base ten followed by a colon and the string.
|
data/Rakefile
CHANGED
data/lib/bencodr/ext.rb
CHANGED
data/lib/bencodr/io.rb
CHANGED
data/lib/bencodr/object.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
1
3
|
module BEncodr
|
2
4
|
module Object
|
3
5
|
def self.bencode(object)
|
@@ -25,7 +27,8 @@ module BEncodr
|
|
25
27
|
end
|
26
28
|
|
27
29
|
def self.bdecode(string)
|
28
|
-
|
30
|
+
scanner = StringScanner.new(string)
|
31
|
+
object = Parser.parse_object(scanner)
|
29
32
|
object or raise BEncodeError, "BEncodr::Object.bdecode was unable to parse the string passed in."
|
30
33
|
end
|
31
34
|
|
data/lib/bencodr/parser.rb
CHANGED
@@ -35,9 +35,10 @@ module BEncodr
|
|
35
35
|
# @param [StringScanner] scanner the scanner of a bencoded string
|
36
36
|
# @return [::String] the parsed string
|
37
37
|
def parse_string(scanner)
|
38
|
-
length = scanner.scan(/[1-9][0-9]*|0/)
|
39
|
-
scanner.scan(/:/)
|
40
|
-
scanner.scan(/.{#{length}}/m)
|
38
|
+
length = scanner.scan(/[1-9][0-9]*|0/) or raise BEncodeError, "Invalid string: length invalid. #{scanner.pos}"
|
39
|
+
scanner.scan(/:/) or raise BEncodeError, "Invalid string: missing colon(:). #{scanner.pos}"
|
40
|
+
byte_string = scanner.scan(/.{#{length}}/m) or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}."
|
41
|
+
byte_string.encode('UTF-8') rescue byte_string.force_encoding('UTF-8')
|
41
42
|
end
|
42
43
|
|
43
44
|
# This method parases a bencoded integer.
|
data/lib/bencodr/version.rb
CHANGED
data/spec/bencode_spec.rb
CHANGED
@@ -27,7 +27,7 @@ describe BEncodr do
|
|
27
27
|
describe "#bdecode_file" do
|
28
28
|
it "should parse a bencoded file" do
|
29
29
|
dirname = File.dirname(__FILE__)
|
30
|
-
BEncodr.bdecode_file("#{dirname}/samples/mini.bencode").should == {"
|
30
|
+
BEncodr.bdecode_file("#{dirname}/samples/mini.bencode").should == {"£" => 3}
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -75,7 +75,7 @@ describe BEncodr do
|
|
75
75
|
context "when parsing and then encoding" do
|
76
76
|
it "should be equal to the pre-parsed and encoded bencoded string" do
|
77
77
|
file = File.dirname(__FILE__) + "/samples/bencode.rb.torrent"
|
78
|
-
BEncodr.bencode(BEncodr.bdecode_file(file)).should == File.open(file, "
|
78
|
+
BEncodr.bencode(BEncodr.bdecode_file(file)).should == File.open(file, "r:UTF-8") {|f| f.read}
|
79
79
|
end
|
80
80
|
end
|
81
81
|
end
|
data/spec/bencodr/io_spec.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
d8:announce38:udp://tracker.publicbt.com:80/announce7:comment11:Awesome! £10:created by13:uTorrent/102013:creation datei1308787427e12:encoded ratei-1e8:encoding5:UTF-84:infod6:lengthi785e4:name10:bencodr.rb12:piece lengthi16384e6:pieces20:�b��F�������O�ګ�ee
|
data/spec/samples/mini.bencode
CHANGED
@@ -1 +1 @@
|
|
1
|
-
d2
|
1
|
+
d2:£i3ee
|
data/spec/shared_examples.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
1
3
|
shared_examples_for "BEncodr::String" do |obj|
|
2
4
|
subject{ obj }
|
3
5
|
|
4
6
|
describe "#bencode" do
|
5
7
|
it{ should bencode("string").to("6:string") }
|
8
|
+
it{ should bencode("£").to("2:£") }
|
6
9
|
it{ should bencode("").to("0:") }
|
7
10
|
it{ should bencode(:symbol).to("6:symbol") }
|
8
11
|
it{ should bencode(URI.parse("http://github.com/blatyo/bencodr")).to("32:http://github.com/blatyo/bencodr") }
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: bencodr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version:
|
5
|
+
version: 3.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Allen Madsen
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- spec/bencodr/string_spec.rb
|
87
87
|
- spec/custom_matchers.rb
|
88
88
|
- spec/samples/bencode.rb.torrent
|
89
|
+
- spec/samples/bencodr.torrent
|
89
90
|
- spec/samples/mini.bencode
|
90
91
|
- spec/samples/python.torrent
|
91
92
|
- spec/shared_examples.rb
|
@@ -130,6 +131,7 @@ test_files:
|
|
130
131
|
- spec/bencodr/string_spec.rb
|
131
132
|
- spec/custom_matchers.rb
|
132
133
|
- spec/samples/bencode.rb.torrent
|
134
|
+
- spec/samples/bencodr.torrent
|
133
135
|
- spec/samples/mini.bencode
|
134
136
|
- spec/samples/python.torrent
|
135
137
|
- spec/shared_examples.rb
|