Implementation of Bencode encoding written in rust

Overview

Rust Bencode

Build Status

Implementation of Bencode encoding written in rust.

Project Status

Not in active developement due to lack of time and other priorities. If you are interested in taking over the developement of this crate please contact me.

What is Bencode?

It is the encoding used by the BitTorrent file sharing system. More on wikipedia.

Using the library

Add this to your Cargo.toml:

[dependencies.bencode]

git = "https://github.com/arjantop/rust-bencode.git"

Documentation

On docs.rs

Contributing

git clone https://github.com/arjantop/rust-bencode
cd rust-bencode
make
Comments
  • Add richer error handling to FromBencode trait.

    Add richer error handling to FromBencode trait.

    This changes the definition of FromBecode to

    trait FromBencode {
        type Err;
        fn from_bencode(bencode: &Bencode) -> Result<Self, Self::Err>;
    }
    

    allowing the from_bencode methods to say why they weren't able to convert from bencode. It also adds error types for the various different FromBencode implementations.

    opened by canndrew 8
  • Does not compile with latest rust (1.0.0-alpha)

    Does not compile with latest rust (1.0.0-alpha)

    I just added this to my Cargo.toml. However on cargo build the compiler fails with this message (after numerous warnings):

    src/bencode.rs:672:5: 672:86 error: method `emit_uint` is not a member of trait `serialize::Encoder`
    src/bencode.rs:672     fn emit_uint(&mut self, v: uint) -> EncoderResult<()> { self.emit_i64(v as i64) }
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src/bencode.rs:682:5: 682:84 error: method `emit_int` is not a member of trait `serialize::Encoder`
    src/bencode.rs:682     fn emit_int(&mut self, v: int) -> EncoderResult<()> { self.emit_i64(v as i64) }
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    src/bencode.rs:988:5: 991:6 error: method `read_uint` is not a member of trait `serialize::Decoder`
    src/bencode.rs:988     fn read_uint(&mut self) -> DecoderResult<uint> {
    src/bencode.rs:989         dec_expect_value!(self);
    src/bencode.rs:990         self.try_read("uint")
    src/bencode.rs:991     }
    src/bencode.rs:1013:5: 1016:6 error: method `read_int` is not a member of trait `serialize::Decoder`
    src/bencode.rs:1013     fn read_int(&mut self) -> DecoderResult<int> {
    src/bencode.rs:1014         dec_expect_value!(self);
    src/bencode.rs:1015         self.try_read("int")
    src/bencode.rs:1016     }
    error: aborting due to 4 previous errors
    Could not compile `bencode`.
    

    Would be appreciated if you could figure that out :)

    opened by Kilobyte22 5
  • Allow `Key` to be constructed manually

    Allow `Key` to be constructed manually

    Tuple structs are now allowed to have private fields which means you can not manually construct a Key with the old syntax.

    This is annoying because scrape responses use the info-hash as dictionary keys.

    opened by andor44 5
  • Question

    Question

    Sorry I opened this just to ask a question... It seems to be the only place to do it.

    Does your library support decoding when some elements might or might not be present? I think this is very important, specially when decoding .torrent files, because there are many optional fields according to the BitTorrent Protocol.

    Another question is: does the library support getting the binary representation of a dictionary? This is needed in order to compute the sha1 hash of the info dictionary contained in .torrent files.

    opened by aochagavia 4
  • Compile error

    Compile error

    I get 77 errors when compiling (with cargo build as well as with make). They look like this:

    src\bencode.rs:935:1: 939:3 note: in expansion of dec_expect_value!
    src\bencode.rs:1189:9: 1189:29 note: expansion site
    src\bencode.rs:936:8: 936:12 error: `self` is not available in a static method.
    Maybe a `self` argument is missing?
    src\bencode.rs:936     if self.expect_key {
                              ^~~~
    src\bencode.rs:935:1: 939:3 note: in expansion of dec_expect_value!
    src\bencode.rs:1197:9: 1197:29 note: expansion site
    

    However, the Travis build passes... How is that possible? I am using the latest rustc nightly.

    I have recreated the issue in a small code snippet on the playpen: http://is.gd/EAiKNk

    opened by aochagavia 3
  • Update for a more recent Rust

    Update for a more recent Rust

    This fixes two classes of compilation errors:

    • use of FromStrRadix, which was removed from std::num recently
    • use of is/us suffixes, which were changed to isize and usize

    to_str_hex was also deprecated and removed. The deprecation note says to use format!. I assume this refers to the {:x} format specifier, but the trait isn't implemented for floats, which is what to_str_hex was being used for. I'm not sure what to do here, so I left it alone in this PR.

    opened by nham 2
  • Show utf8 bytestrings as strings

    Show utf8 bytestrings as strings

    This changes the implementation of Show for util::ByteString and Bencode::ByteString so that when they contain valid utf8 they are shown as strings. ie. s"foo" instead of s[102, 111, 111].

    opened by canndrew 2
  • Make travis happy again by fixing Cargo.toml

    Make travis happy again by fixing Cargo.toml

    'lib' in Cargo.toml is expected to be an array [[lib]], not table [lib](thanks for Toml developer for 'obvious' format).

    Both variants pass locally but for mysterious reasons table one fails on Travis.

    Successful build: https://travis-ci.org/Divius/rust-bencode

    opened by dtantsur 2
  • Release a new version

    Release a new version

    Hi,

    PR #38 introduced a fix for current Rust versions. Could you make a new release and publish it to crates.io so we can use it without pointing to git?

    Thanks!

    opened by progval 1
  • Fix build error

    Fix build error

    Using rust 0.10, "make" was failing with this output:

    mkdir -p build
    mkdir -p build/test
    rustc --test -o build/test/test src/bencode.rs
    src/streaming.rs:34:16: 34:31 error: private type in exported type signature, #[deny(visible_private_types)] on by default
    src/streaming.rs:34     stack: Vec<BencodePosition>,
                                       ^~~~~~~~~~~~~~~
    error: aborting due to previous error
    make: *** [libtest] Error 101
    
    opened by marksamman 1
  • I need an easy/efficient way to calculate the sha1 of a dictionary.

    I need an easy/efficient way to calculate the sha1 of a dictionary.

    The way I was thinking of implementing this is to create a BencodeSlice<'a> type that serves a similar role to Bencode except that it keeps the encoded data as a slice into the buffer from which it was parsed. Something like this:

    pub struct BencodeSlice<'a> {
        encoded: &'a [u8],
        decoded: BencodeSliceEntry<'a>,
    }
    
    pub enum BencodeSliceEntry<'a> {
        Empty,
        Number(i64),
        ByteString(&'a [u8]),
        List(Vec<BencodeSlice<'a>>),
        Dict(TreeMap<&'a [u8], BencodeSlice<'a>>),
    }
    

    You could then have a FromBencodeSlice trait that takes a BencodeSlice instead of a Bencode and I could use the encoded field of the BencodeSlice to calculate the sha1 info-hash of a torrent.

    This would also have the advantage of making it slightly more efficient to parse and process bencoded data because it would eliminate the need to allocate unnecessarily for strings and dictionary keys.

    The disadvantage of this is it would require rewriting the meat of the library so that the parser processes a &[u8] instead of an Iterator<u8>. In other words, replacing the streaming and BencodeEvent stuff with a different implementation. The only other library I can find on github that uses this library doesn't use the BencodeEvent stuff so I don't whether this would be an issue or not.

    Would you (@aranjtop) accept a pull request that makes such sweeping changes to this library? Or should I just write another bencode library? I'd rather not write another library as this library is already pretty good and I don't like there being multiple rust library for doing the same thing.

    opened by canndrew 3
  • Name conflict between 2 ByteString's

    Name conflict between 2 ByteString's

    There are 2 things in this library called ByteString: enum item ByteString and type util::ByteString used as a key. Placing in util:: mod looks like it's something auxiliary while actually it's necessary part of the core functionality (I can hardly imagine implementing To/FromBencode without using it explicitly). I am working around this like:

    use bencode;
    use bencode::util::ByteString;
    ...
    

    Now I have ByteString and bencode::ByteString, both from bencode package. Guess which one is which :) Judging by use case, the could should be Key (why you didn't use it as initally wanted?) or smth like BytesHelper.

    Could you rename one of ByteString's and move the second one to the top level of your package?

    opened by dtantsur 6
  • Reading a string which contains invalid UTF8

    Reading a string which contains invalid UTF8

    In a .torrent file, the hashes of the pieces are saved as a string. However, this string does not match Rust's String type, because it contains incorrect UTF8. What we actually want is to save the hashes in a Vec<u8>, but then the library will try to parse a list and produce an error.

    Is there a work-around for this?

    opened by aochagavia 11
Owner
Arjan Topolovec
Arjan Topolovec
Encoding and decoding support for BSON in Rust

bson-rs Encoding and decoding support for BSON in Rust Index Overview of BSON Format Usage BSON Values BSON Documents Modeling BSON with strongly type

mongodb 304 Dec 30, 2022
Character encoding support for Rust

Encoding 0.3.0-dev Character encoding support for Rust. (also known as rust-encoding) It is based on WHATWG Encoding Standard, and also provides an ad

Kang Seonghoon 264 Dec 14, 2022
A HTML entity encoding library for Rust

A HTML entity encoding library for Rust Example usage All example assume a extern crate htmlescape; and use htmlescape::{relevant functions here}; is

Viktor Dahl 41 Nov 1, 2022
A TOML encoding/decoding library for Rust

toml-rs A TOML decoder and encoder for Rust. This library is currently compliant with the v0.5.0 version of TOML. This library will also likely contin

Alex Crichton 1k Dec 30, 2022
Variable-length signed and unsigned integer encoding that is byte-orderable for Rust

ordered-varint Provides variable-length signed and unsigned integer encoding that is byte-orderable. This crate provides the Variable trait which enco

Khonsu Labs 7 Dec 6, 2022
A series of compact encoding schemes for building small and fast parsers and serializers

A series of compact encoding schemes for building small and fast parsers and serializers

Manfred Kröhnert 2 Feb 5, 2022
Astro Format is a library for efficiently encoding and decoding a set of bytes into a single buffer format.

Astro Format is a library for efficiently transcoding arrays into a single buffer and native rust types into strings

Stelar Labs 1 Aug 13, 2022
Entropy Encoding notebook. Simple implementations of the "tANS" encoder/decoder.

EntropyEncoding Experiments This repository contains my Entropy Encoding notebook. Entropy encoding is an efficient lossless data compression scheme.

Nadav Rotem 4 Dec 21, 2022
TLV-C encoding support.

TLV-C: Tag - Length - Value - Checksum TLV-C is a variant on the traditional [TLV] format that adds a whole mess of checksums and whatnot. Why, you as

Oxide Computer Company 3 Nov 25, 2022
MessagePack implementation for Rust / msgpack.org[Rust]

RMP - Rust MessagePack RMP is a pure Rust MessagePack implementation. This repository consists of three separate crates: the RMP core and two implemen

Evgeny Safronov 840 Dec 30, 2022
Rust implementation of CRC(16, 32, 64) with support of various standards

crc Rust implementation of CRC(16, 32, 64). MSRV is 1.46. Usage Add crc to Cargo.toml [dependencies] crc = "2.0" Compute CRC use crc::{Crc, Algorithm,

Rui Hu 120 Dec 23, 2022
PROST! a Protocol Buffers implementation for the Rust Language

PROST! prost is a Protocol Buffers implementation for the Rust Language. prost generates simple, idiomatic Rust code from proto2 and proto3 files. Com

Dan Burkert 17 Jan 8, 2023
Rust implementation of Google protocol buffers

rust-protobuf Protobuf implementation in Rust. Written in pure rust Generate rust code Has runtime library for generated code (Coded{Input|Output}Stre

Stepan Koltsov 2.3k Dec 31, 2022
A fast, performant implementation of skip list in Rust.

Subway A fast, performant implementation of skip list in Rust. A skip list is probabilistic data structure that provides O(log N) search and insertion

Sushrut 16 Apr 5, 2022
Pure Rust port of CRFsuite: a fast implementation of Conditional Random Fields (CRFs)

crfs-rs Pure Rust port of CRFsuite: a fast implementation of Conditional Random Fields (CRFs) Currently only support prediction, model training is not

messense 24 Nov 23, 2022
A binary encoder / decoder implementation in Rust.

Bincode A compact encoder / decoder pair that uses a binary zero-fluff encoding scheme. The size of the encoded object will be the same or smaller tha

Bincode 1.9k Dec 29, 2022
A HTTP Archive format (HAR) serialization & deserialization library, written in Rust.

har-rs HTTP Archive format (HAR) serialization & deserialization library, written in Rust. Install Add the following to your Cargo.toml file: [depende

Sebastian Mandrean 25 Dec 24, 2022
rust-jsonnet - The Google Jsonnet( operation data template language) for rust

rust-jsonnet ==== Crate rust-jsonnet - The Google Jsonnet( operation data template language) for rust Google jsonnet documet: (http://google.github.io

Qihoo 360 24 Dec 1, 2022
A Rust ASN.1 (DER) serializer.

rust-asn1 This is a Rust library for parsing and generating ASN.1 data (DER only). Installation Add asn1 to the [dependencies] section of your Cargo.t

Alex Gaynor 85 Dec 16, 2022