github github
  • Home
  • Pricing and Signup
  • Training
  • Gist
  • Blog
  • Login

blatyo / bencodr

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 3
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (4)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Switch Branches (2)
    • gh-pages
    • master ✓
  • Switch Tags (4)
    • v1.2.0
    • v1.1.0
    • v1.0.1
    • v1.0.0
  • Branch List
Sending Request…

This gem provides a way to encode and parse bencodings used by the Bit Torrent protocol. — Read more

  Cancel

http://blatyo.github.com/bencodr

  Cancel
  • HTTP
  • Git Read-Only

This URL has Read+Write access

Regenerated gemspec for version 1.2.0 
blatyo (author)
Thu Feb 04 09:37:13 -0800 2010
commit  8144ec5e12951519d617
tree    037e859b0224477d62bd
parent  39acf2e518aace1189ab
bencodr /
name age
history
message
file .autotest Sat Jan 30 09:01:01 -0800 2010 Added autotest stuff to make testing more enjoy... [blatyo]
file .document Tue Jan 19 11:14:25 -0800 2010 Initial commit to bencode. [blatyo]
file .gitignore Sat Jan 30 10:27:44 -0800 2010 Added decode and file decode. [blatyo]
file LICENSE Tue Jan 19 11:14:25 -0800 2010 Initial commit to bencode. [blatyo]
file README.rdoc Thu Feb 04 09:36:06 -0800 2010 Added IO tests and the like. [blatyo]
file Rakefile Tue Feb 02 15:30:10 -0800 2010 Renamed gem [blatyo]
file VERSION Thu Feb 04 09:36:53 -0800 2010 Version bump to 1.2.0 [blatyo]
directory autotest/ Sat Jan 30 09:01:01 -0800 2010 Added autotest stuff to make testing more enjoy... [blatyo]
file bencodr.gemspec Thu Feb 04 09:37:13 -0800 2010 Regenerated gemspec for version 1.2.0 [blatyo]
directory lib/ Thu Feb 04 09:36:06 -0800 2010 Added IO tests and the like. [blatyo]
directory spec/ Thu Feb 04 09:36:06 -0800 2010 Added IO tests and the like. [blatyo]
README.rdoc

BEncodr

  • Author Allen Madsen (blatyo)
  • My Site www.allenmadsen.com
  • Gem gemcutter.org/gems/bencodr
  • Source github.com/blatyo/bencodr
  • Documentation blatyo.github.com/bencodr
  • Issue Tracker github.com/blatyo/bencodr/issues

Synopsis

This gem provides a way to encode and parse bencodings used by the Bit Torrent protocol.

Installation

    # install the gem
    > [sudo] gem install bencodr
    Successfully installed bencodr
    1 gem installed
    Installing ri documentation for bencodr...
    Building YARD (yri) index for bencodr...
    Installing RDoc documentation for bencodr

    # somefile.rb
    require 'bencodr'

Examples

String

BEncoded strings are length-prefixed base ten followed by a colon and the string.

    # strings
    "".bencode              #=> "0:"
    "string".bencode        #=> "6:string"

    # symbols
    :symbol.bencode         #=> "6:symbol"

    # URIs
    uri = URI.parse("http://github.com/blatyo/bencode")
    uri.bencode             #=> "32:http://github.com/blatyo/bencode"

Integer

Bencoded integers are represented by an ‘i’ followed by the number in base 10 followed by an ‘e’.

    # integers
    1.bencode               #=> "i1e"
    -1.bencode              #=> "i-1e"
    10_000_000_000.bencode  #=> "i10000000000e"

    # other numerics
    1.1.bencode             #=> "i1e"
    -1e10.bencode           #=> "i-10000000000e"

    # times
    Time.at(4).bencode      #=> "i4e"

List

Bencoded lists are encoded as an ‘l’ followed by their elements (also bencoded) followed by an ‘e’.

    # arrays
    [].bencode                        #=> "le"
    [:e, "a", 1, Time.at(11)].bencode #=> "l1:e1:ai1ei11ee"

Dictionary

Bencoded dictionaries are encoded as a ‘d’ followed by a list of alternating keys and their corresponding values followed by an ‘e’. Keys appear in sorted order (sorted as raw strings, not alphanumerics) and are always strings.

    # hashes
    {}.bencode                          #=> "de"
    {"string" => "string"}.bencode      #=> "d6:string6:stringe"
    {:symbol => :symbol}.bencode        #=> "d6:symbol6:symbole"
    {1 => 1}.bencode                    #=> "d1:1i1ee"
    {1.1 => 1.1}.bencode                #=> "d3:1.1i1ee"
    {{} => {}}.bencode                  #=> "d2:{}dee"

    time = Time.utc(0)
    {time => time}.bencode              #=> "d23:2000-01-01 00:00:00 UTCi946684800ee"

    array = (1..4).to_a
    {array => array}.bencode            #=> "d12:[1, 2, 3, 4]li1ei2ei3ei4eee"

    # Note: keys are sorted as raw strings.
    {:a => 1, "A" => 1, 1=> 1}.bencode  #=> "d1:1i1e1:Ai1e1:ai1ee"

Decoding

You can decode a bencoding by calling bdecode on the string.

    "6:string".bdecode  #=> "string"
    "i1e".bdecode       #=> 1
    "le".bdecode        #=> []
    "de".bdecode        #=> {}

IO and Files

You can also write and read bencodings.

    # write to standard out
    IO.bencode(1, "string")             #=> "6:string" to stdout
    $stdout.bencode("string")           #=> "6:string" to stdout

    # write to file
    File.bencode("a.bencode", "string") #=> "6:string" to a.bencode

    file = File.open("a.bencode", "wb")
    file.bencode("string")              #=> "6:string" to a.bencode

    # read from standard in
    IO.bdecode(0)                       #=> "string"
    $stdin.bdecode                      #=> "string"

    # read from file
    File.bdecode("a.bencode")           #=> "string"

    file = File.open("a.bencode", "wb")
    file.bdecode                        #=> "string"

BEncodr

Most of the functionality of this library can also be accessed directly on the BEncodr class.

    # encoding is just like calling bencode on the object
    BEncodr.encode("string")    #=> "6:string"

    # decoding is just like calling bdecode on a bencoding
    BEncodr.decode("6:string")  #=> "string"

    # you can work directly with files too
    BEncodr.encode_file("my_awesome.torrent", {:announce => "http://www.sometracker.com/announce:80"})
    BEncodr.decode_file("my_awesome.torrent") #=> {:announce => "http://www.sometracker.com/announce:80"}

Registering Types

When using bencodings it may be useful to translate your own objects into bencoded strings. You can do that with the register methods on each BEncode type. All register does is define a bencode instance method for the class that internally uses type conversion. That means if you want to specify a String type then your class must have a to_s or to_str instance method. The same goes for all the other types.

Keep in mind when registering that if you register a class for two separate types it will only retain the bencode method of the last type registered for.

    # register string type
    BEncodr::String.register Range
    (1..2).bencode      #=> "4:1..2"

    # register integer type
    BEncodr::Integer.register NilClass
    nil.bencode         #=> "i0e"

    # register list type
    BEncodr::List.register Range
    (1..2).bencode      #=> "li1ei2ee"

    #register dictionary type
    MyClass = Class.new do
      def to_h
        {:a => "a", :b => "b"}
      end
    end
    BEncodr::Dictionary.register MyClass
    MyClass.new.bencode #=> "d1:a1:a1:b1:be"

Note on Reporting Issues

  • Try to make a failing test case
  • Tell me which version of ruby you’re using
  • Tell me which OS you are using
  • Provide me with any extra files if necessary

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2010 Allen Madsen. See LICENSE for details.

Dedicated Server Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
  • Blog
  • Support
  • Training
  • Job Board
  • Shop
  • Contact
  • API
  • Status
  • © 2010 GitHub Inc. All rights reserved.
  • Terms of Service
  • Privacy
  • Security
  • English
  • Deutsch
  • Français
  • 日本語
  • Português (BR)
  • 中文
  • See all available languages

Your current locale selection: English. Choose another?

  • English
  • Afrikaans
  • Català
  • Čeština
  • Deutsch
  • Español
  • Français
  • Hrvatski
  • Indonesia
  • Italiano
  • 日本語
  • Nederlands
  • Norsk
  • Polski
  • Português (BR)
  • Српски
  • Svenska
  • 中文