Skip to content

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

@canndrew

Description

@canndrew

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions