rust-bencode icon indicating copy to clipboard operation
rust-bencode copied to clipboard

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

Open canndrew opened this issue 11 years ago • 3 comments

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.

canndrew avatar Nov 14 '14 08:11 canndrew

Actually nevermind. My plan would make it more difficult and inefficient to parse bencoded data from a reader (which is what people probably usually want to do). I'll rethink things.

canndrew avatar Nov 14 '14 10:11 canndrew

The problem of calculating a hash came up before (https://github.com/arjantop/rust-bencode/issues/7), I agree that a better solution would be nice is a good solution is found.

I would gladly accept bigger changes to the library if they are first discussed here with feedback from the other users that might get affected by the changes.

arjantop avatar Nov 15 '14 10:11 arjantop

I've implemented what I mentioned in the first comment on a branch http://github.com/canndrew/rust-bencode/tree/borrowed .

It adds an extra BorrowedBencode<'s> type but leaves the existing Bencode type in place. Would you be interested in merging something like this? The code could do with being cleaned up, have tests added, factor out duplicated code etc. but I could get round to doing that if you're interested.

canndrew avatar Apr 17 '15 07:04 canndrew