bencoding 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/library/bencoding.rb +10 -0
- data/library/bencoding/bdecode.rb +64 -0
- data/library/bencoding/bencode.rb +16 -0
- metadata +68 -0
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Most of this code is taken from https://github.com/dasch/ruby-bencode/blob/master/lib/bencode/decode.rb
|
4
|
+
# Thanks for contributing it, dasch!
|
5
|
+
|
6
|
+
require 'strscan'
|
7
|
+
|
8
|
+
module Bencoding
|
9
|
+
class DecodeError < StandardError; end
|
10
|
+
|
11
|
+
module_function
|
12
|
+
|
13
|
+
def decode_file path
|
14
|
+
decode File.open(path, 'rb').read
|
15
|
+
end
|
16
|
+
|
17
|
+
def decode string
|
18
|
+
scanner = StringScanner.new string
|
19
|
+
parse scanner
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse scanner
|
23
|
+
case scanner.peek(1)[0]
|
24
|
+
when ?i
|
25
|
+
scanner.pos += 1
|
26
|
+
number = scanner.scan_until(/e/) or raise DecodeError
|
27
|
+
number.chop.to_i
|
28
|
+
when ?l
|
29
|
+
scanner.pos += 1
|
30
|
+
|
31
|
+
Array.new.tap do |result|
|
32
|
+
result << parse(scanner) until scanner.scan /e/
|
33
|
+
end
|
34
|
+
when ?d
|
35
|
+
scanner.pos += 1
|
36
|
+
|
37
|
+
Hash.new.tap do |result|
|
38
|
+
until scanner.scan(/e/)
|
39
|
+
key, value = parse(scanner), parse(scanner)
|
40
|
+
|
41
|
+
raise DecodeError, "invalid key for dictionary" unless key.is_a? String or key.is_a? Fixnum
|
42
|
+
|
43
|
+
result.store "#{key}", value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
when ?0 .. ?9
|
47
|
+
length = scanner.scan_until(/:/) or raise DecodeError, "invalid string"
|
48
|
+
|
49
|
+
begin
|
50
|
+
length = length.chop.to_i
|
51
|
+
string = scanner.peek length
|
52
|
+
scanner.pos += length
|
53
|
+
rescue RangeError
|
54
|
+
raise DecodeError, "invalid string length"
|
55
|
+
end
|
56
|
+
|
57
|
+
string
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class String
|
63
|
+
def bdecode; Bencoding.decode self end
|
64
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
class Array; def bencode; [?l, map(&:bencode), ?e].join end end
|
4
|
+
class String; def bencode; [length, ?:, self].join end end
|
5
|
+
class Fixnum; def bencode; [?i, self, ?e].join end end
|
6
|
+
class Object; def bencode; to_s.bencode end end
|
7
|
+
class TrueClass; def bencode; 1.bencode end end
|
8
|
+
class FalseClass; def bencode; 0.bencode end end
|
9
|
+
|
10
|
+
class Hash
|
11
|
+
def bencode
|
12
|
+
map do |key, value|
|
13
|
+
[key.to_s.gsub(?_, ' '), value].map &:bencode
|
14
|
+
end.unshift(?d).push(?e).join
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bencoding
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Mikkel Kroman
|
13
|
+
autorequire:
|
14
|
+
bindir: executables
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-12 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: mk@maero.dk
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- library/bencoding.rb
|
31
|
+
- library/bencoding/bencode.rb
|
32
|
+
- library/bencoding/bdecode.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage:
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- library
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
segments:
|
48
|
+
- 1
|
49
|
+
- 9
|
50
|
+
- 1
|
51
|
+
version: 1.9.1
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: BitTorrent encoding (bencoding) support for Ruby.
|
67
|
+
test_files: []
|
68
|
+
|