Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: quaternion/bencodr
base: fix_old_rubies
...
head fork: blatyo/bencodr
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 14 commits
  • 9 files changed
  • 0 commit comments
  • 3 contributors
View
2  .rvmrc
@@ -1 +1 @@
-rvm --create use 1.9.2@bencodr
+rvm --create use 1.9.3@bencodr
View
5 README.md
@@ -4,6 +4,7 @@
* **Gem** http://gemcutter.org/gems/bencodr
* **Source** http://github.com/blatyo/bencodr
* **Issue Tracker** http://github.com/blatyo/bencodr/issues
+* **Build Status** [![Build Status](http://travis-ci.org/blatyo/bencodr.png)](http://travis-ci.org/blatyo/bencodr)
## Synopsis
This gem provides a way to encode and parse bencodings used by the Bit Torrent protocol.
@@ -195,6 +196,10 @@ When using bencodings it may be useful to translate your own objects into bencod
MyClass.new.bencode #=> "d1:a1:a1:b1:be"
```
+## Contributors
+* [Andrew Nikolaev](https://github.com/quaternion) - Fixed encoding issue
+* [Tarangp](https://github.com/Tarangp) - Fixed parsing of long bencoded strings
+
## Note on Reporting Issues
* Try to make a failing test case
View
7 lib/bencodr/parser.rb
@@ -35,9 +35,12 @@ def parse_object(scanner)
# @param [StringScanner] scanner the scanner of a bencoded string
# @return [::String] the parsed string
def parse_string(scanner)
- length = scanner.scan(/[1-9][0-9]*|0/) or raise BEncodeError, "Invalid string: length invalid. #{scanner.pos}"
+ length = scanner.scan(/[1-9][0-9]*|0/).to_i or raise BEncodeError, "Invalid string: length invalid. #{scanner.pos}"
scanner.scan(/:/) or raise BEncodeError, "Invalid string: missing colon(:). #{scanner.pos}"
- byte_string = scanner.scan(/.{#{length}}/m) or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}."
+ byte_string = scanner.peek(length)
+ byte_string.length == length or raise BEncodeError, "Invalid string: length too long(#{length}) #{scanner.pos}."
+ scanner.pos = scanner.pos + length
+
if RUBY_VERSION =~ /1\.9/
byte_string.encode('UTF-8') rescue byte_string.force_encoding('UTF-8')
else
View
2  lib/bencodr/version.rb
@@ -1,3 +1,3 @@
module BEncodr
- VERSION = "3.0.0"
+ VERSION = "3.0.2"
end
View
12 spec/bencode_spec.rb
@@ -70,12 +70,18 @@
file = File.join(File.dirname(__FILE__), 'samples', 'python.torrent')
lambda{BEncodr.bdecode_file file}.should_not raise_error
end
+
+ it "should read a torrent with long strings without raising an exception" do
+ file = File.join(File.dirname(__FILE__), 'samples', 'big_torrent.torrent')
+ lambda{BEncodr.bdecode_file file}.should_not raise_error
+ end
end
context "when parsing and then encoding" do
- it "should be equal to the pre-parsed and encoded bencoded string" do
- file = File.dirname(__FILE__) + "/samples/bencode.rb.torrent"
- BEncodr.bencode(BEncodr.bdecode_file(file)).should == File.open(file, "r:UTF-8") {|f| f.read}
+ Dir[File.dirname(__FILE__) + "/samples/*.torrent"].each do |file|
+ it "should be equal to the pre-parsed and encoded bencoded string for #{file}" do
+ BEncodr.bencode(BEncodr.bdecode_file(file)).should == File.open(file, "r:UTF-8") {|f| f.read}
+ end
end
end
end
View
4 spec/bencodr/parser_spec.rb
@@ -52,6 +52,10 @@
end
describe String do
+ before :all do
+ BEncodr::Ext.include!
+ end
+
describe "#bdecode" do
it{ "6:string".should bdecode_to("string") }
it{ "i-1e".should bdecode_to(-1) }
View
1  spec/samples/big_torrent.torrent
1 addition, 0 deletions not shown
View
41 spec/shared_examples.rb
@@ -19,7 +19,7 @@
shared_examples_for "BEncodr::Integer" do |obj|
subject{ obj }
- describe "#bencodr" do
+ describe "#bencode" do
it{ should bencode(1).to("i1e") }
it{ should bencode(-1).to("i-1e") }
it{ should bencode(10_000_000_000).to("i10000000000e") }
@@ -53,21 +53,38 @@
it{ should bencode({:symbol => :symbol}).to("d6:symbol6:symbole") }
it{ should bencode({1 => 1}).to("d1:1i1ee")}
it{ should bencode({1.1 => 1.1}).to("d3:1.1i1ee") }
- it{ should bencode({{} => {}}).to("d2:{}dee") }
- it{
- uri = URI.parse("http://github.com/blatyo/bencode")
- should bencode({uri => uri}).to("d32:http://github.com/blatyo/bencode32:http://github.com/blatyo/bencodee")
- }
+ describe "ruby 1.9.x", :if => $ruby19 do
+ it{ should bencode({{} => {}}).to("d2:{}dee") }
- it{
- time = Time.utc(0)
- should bencode({time => time}).to("d23:0000-01-01 00:00:00 UTCi-62167219200ee")
- }
+ it{
+ time = Time.utc(0)
+ should bencode({time => time}).to("d23:0000-01-01 00:00:00 UTCi-62167219200ee")
+ }
+
+ it{
+ array = (1..4).to_a
+ should bencode({array => array}).to("d12:[1, 2, 3, 4]li1ei2ei3ei4eee")
+ }
+ end
+
+ describe "ruby 1.8.x", :unless => $ruby19 do
+ it{ should bencode({{} => {}}).to("d0:dee") }
+
+ it{
+ time = Time.utc(0)
+ should bencode({time => time}).to("d28:Sat Jan 01 00:00:00 UTC 2000i946684800ee")
+ }
+
+ it{
+ array = (1..4).to_a
+ should bencode({array => array}).to("d4:1234li1ei2ei3ei4eee")
+ }
+ end
it{
- array = (1..4).to_a
- should bencode({array => array}).to("d12:[1, 2, 3, 4]li1ei2ei3ei4eee")
+ uri = URI.parse("http://github.com/blatyo/bencode")
+ should bencode({uri => uri}).to("d32:http://github.com/blatyo/bencode32:http://github.com/blatyo/bencodee")
}
end
end
View
2  spec/spec_helper.rb
@@ -5,6 +5,8 @@
require 'custom_matchers'
require 'shared_examples'
+$ruby19 = RUBY_VERSION =~ /1\.9/
+
RSpec.configure do |c|
c.formatter = Fuubar
c.color_enabled = true

No commit comments for this range

Something went wrong with that request. Please try again.