Skip to main content

find_dict_key_span

Function find_dict_key_span 

Source
pub fn find_dict_key_span(data: &[u8], key: &str) -> Result<Range<usize>>
Expand description

Find the raw byte span of a value for a given key in a bencoded dictionary.

This is critical for info-hash computation: the info-hash is the SHA1 of the original raw bytes of the “info” dictionary value, not a re-serialized copy.

Returns the byte range start..end of the value associated with key.

§Example

use irontide_bencode::find_dict_key_span;

let data = b"d4:infod4:name4:test12:piece lengthi1024eee";
let span = find_dict_key_span(data, "info").unwrap();
assert_eq!(&data[span.clone()], b"d4:name4:test12:piece lengthi1024ee");

§Errors

Returns an error if the data is not a valid bencoded dictionary or the key is not found.