// Torrents are composed of "pieces".
// Pieces have a 20-byte SHA-1 hash.
// Pieces are composed of an undocumented subunit called "chunks".
// When a peer wants a piece, the peer has to ask for:
// - The piece index
// - The chunk offset within the piece (starting at byte 0)
// - The length of the chunk
Torrents are composed of "pieces".
Pieces have a 20-byte SHA-1 hash.
Pieces are composed of an undocumented subunit called "chunks".
When a peer wants a piece, the peer has to ask for:
The piece index
The chunk offset within the piece (starting at byte 0)
The length of the chunk
Torrents are composed of " pieces " . Pieces have a 20 - byte SHA - 1 hash . Pieces are composed of an undocumented subunit called " chunks " . When a peer wants a piece the peer has to ask for : The piece index The chunk offset within the piece ( starting at byte 0 ) The length of the chunk
pub(crate) fn chunk_offsets_lengths(
    piece_length: usize,
    chunk_length: usize,
) -> Vec<(usize, usize)> {
    let mut remaining_length = piece_length;
    let mut i = 0;
    let mut offsets = vec![];
    let chunks_per_piece = piece_length / chunk_length;
    while i < chunks_per_piece {
        let offset = chunk_length * i;
        offsets.push((offset, chunk_length));
        i += 1;
        remaining_length -= chunk_length;
    }
    if remaining_length > 0 {
        let offset = chunk_length * i;
        offsets.push((offset, remaining_length));
    }
    offsets
}
pub ( crate ) fn chunk_offsets_lengths ( piece_length : usize , chunk_length : usize , ) -> Vec < ( usize , usize ) > { let mut remaining_length = piece_length ; let mut i = 0 ; let mut offsets = vec ! [ ] ; let chunks_per_piece = piece_length / chunk_length ; while i < chunks_per_piece { let offset = chunk_length * i ; offsets . push ( ( offset , chunk_length ) ) ; i += 1 ; remaining_length -= chunk_length ; } if remaining_length > 0 { let offset = chunk_length * i ; offsets . push ( ( offset , remaining_length ) ) ; } offsets }
Torrents are composed of "pieces".
Torrents are composed of " pieces " .
piece_length
usize
chunk_length
usize