2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2021-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2021-11-24 14:48:52 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
|
2022-02-23 13:38:18 +00:00
|
|
|
#include "tr-assert.h"
|
|
|
|
|
2021-11-24 14:48:52 +00:00
|
|
|
struct tr_block_info
|
|
|
|
{
|
2022-02-23 20:25:06 +00:00
|
|
|
static auto constexpr BlockSize = uint32_t{ 1024 * 16 };
|
|
|
|
|
2021-11-24 14:48:52 +00:00
|
|
|
uint64_t total_size = 0;
|
|
|
|
uint64_t piece_size = 0;
|
|
|
|
uint64_t n_pieces = 0;
|
|
|
|
|
|
|
|
tr_block_index_t n_blocks = 0;
|
|
|
|
uint32_t final_block_size = 0;
|
|
|
|
uint32_t final_piece_size = 0;
|
|
|
|
|
|
|
|
tr_block_info() = default;
|
2021-11-25 18:26:51 +00:00
|
|
|
tr_block_info(uint64_t total_size_in, uint64_t piece_size_in)
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2021-11-25 18:26:51 +00:00
|
|
|
initSizes(total_size_in, piece_size_in);
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void initSizes(uint64_t total_size_in, uint64_t piece_size_in);
|
|
|
|
|
2022-01-07 19:13:37 +00:00
|
|
|
[[nodiscard]] constexpr auto blockCount() const
|
|
|
|
{
|
|
|
|
return n_blocks;
|
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
// return the number of bytes in `block`
|
2022-01-07 19:13:37 +00:00
|
|
|
[[nodiscard]] constexpr auto blockSize(tr_block_index_t block) const
|
|
|
|
{
|
2022-02-23 20:25:06 +00:00
|
|
|
return block + 1 == n_blocks ? final_block_size : BlockSize;
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto pieceCount() const
|
|
|
|
{
|
|
|
|
return n_pieces;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto pieceSize() const
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-01-07 19:13:37 +00:00
|
|
|
return piece_size;
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
// return the number of bytes in `piece`
|
2022-01-07 19:13:37 +00:00
|
|
|
[[nodiscard]] constexpr auto pieceSize(tr_piece_index_t piece) const
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-01-07 19:13:37 +00:00
|
|
|
return piece + 1 == n_pieces ? final_piece_size : pieceSize();
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
[[nodiscard]] constexpr tr_block_span_t blockSpanForPiece(tr_piece_index_t piece) const
|
2021-11-25 18:26:51 +00:00
|
|
|
{
|
2022-02-18 23:17:19 +00:00
|
|
|
if (!isInitialized())
|
2021-12-06 03:12:21 +00:00
|
|
|
{
|
2022-02-18 23:17:19 +00:00
|
|
|
return {};
|
2021-12-06 03:12:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
return { pieceLoc(piece).block, pieceLastLoc(piece).block + 1 };
|
|
|
|
}
|
2021-12-06 03:12:21 +00:00
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
[[nodiscard]] constexpr auto totalSize() const
|
|
|
|
{
|
|
|
|
return total_size;
|
2021-11-25 18:26:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
struct Location
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-02-18 23:17:19 +00:00
|
|
|
uint64_t byte = 0;
|
|
|
|
|
|
|
|
tr_piece_index_t piece = 0;
|
|
|
|
uint32_t piece_offset = 0;
|
|
|
|
|
|
|
|
tr_block_index_t block = 0;
|
|
|
|
uint32_t block_offset = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] bool operator==(Location const& that) const
|
2021-12-06 03:12:21 +00:00
|
|
|
{
|
2022-02-18 23:17:19 +00:00
|
|
|
return this->byte == that.byte;
|
2021-12-06 03:12:21 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
[[nodiscard]] bool operator<(Location const& that) const
|
2021-12-06 03:12:21 +00:00
|
|
|
{
|
2022-02-18 23:17:19 +00:00
|
|
|
return this->byte < that.byte;
|
2021-12-06 03:12:21 +00:00
|
|
|
}
|
2022-02-18 23:17:19 +00:00
|
|
|
};
|
2021-12-06 03:12:21 +00:00
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
// Location of the first byte in `block`.
|
|
|
|
[[nodiscard]] Location constexpr blockLoc(tr_block_index_t block) const
|
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
TR_ASSERT(block < n_blocks);
|
|
|
|
|
2022-02-23 20:25:06 +00:00
|
|
|
return byteLoc(uint64_t{ block } * BlockSize);
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
// Location of the last byte in `block`.
|
|
|
|
[[nodiscard]] Location constexpr blockLastLoc(tr_block_index_t block) const
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-02-18 23:17:19 +00:00
|
|
|
if (!isInitialized())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-02-23 20:25:06 +00:00
|
|
|
return byteLoc(uint64_t{ block } * BlockSize + blockSize(block) - 1);
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
// Location of the first byte (+ optional offset and length) in `piece`
|
|
|
|
[[nodiscard]] Location constexpr pieceLoc(tr_piece_index_t piece, uint32_t offset = 0, uint32_t length = 0) const
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-02-23 13:38:18 +00:00
|
|
|
TR_ASSERT(piece < n_pieces);
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
return byteLoc(uint64_t{ piece } * pieceSize() + offset + length);
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
// Location of the last byte in `piece`.
|
|
|
|
[[nodiscard]] Location constexpr pieceLastLoc(tr_piece_index_t piece) const
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-02-18 23:17:19 +00:00
|
|
|
if (!isInitialized())
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
return byteLoc(uint64_t{ piece } * pieceSize() + pieceSize(piece) - 1);
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
// Location of the torrent's nth byte
|
2022-03-26 03:39:08 +00:00
|
|
|
[[nodiscard]] Location constexpr byteLoc(uint64_t byte_idx) const
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2022-03-26 03:39:08 +00:00
|
|
|
TR_ASSERT(byte_idx <= total_size);
|
2022-02-23 13:38:18 +00:00
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
if (!isInitialized())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto loc = Location{};
|
|
|
|
|
2022-03-26 03:39:08 +00:00
|
|
|
loc.byte = byte_idx;
|
2022-02-18 23:17:19 +00:00
|
|
|
|
2022-03-26 03:39:08 +00:00
|
|
|
if (byte_idx == totalSize()) // handle 0-byte files at the end of a torrent
|
2022-02-18 23:17:19 +00:00
|
|
|
{
|
|
|
|
loc.block = blockCount() - 1;
|
|
|
|
loc.piece = pieceCount() - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-26 03:39:08 +00:00
|
|
|
loc.block = byte_idx / BlockSize;
|
|
|
|
loc.piece = byte_idx / pieceSize();
|
2022-02-18 23:17:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-23 20:25:06 +00:00
|
|
|
loc.block_offset = static_cast<uint32_t>(loc.byte - (uint64_t{ loc.block } * BlockSize));
|
2022-02-18 23:17:19 +00:00
|
|
|
loc.piece_offset = static_cast<uint32_t>(loc.byte - (uint64_t{ loc.piece } * pieceSize()));
|
|
|
|
|
|
|
|
return loc;
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 23:17:19 +00:00
|
|
|
[[nodiscard]] static uint32_t bestBlockSize(uint64_t piece_size);
|
|
|
|
|
|
|
|
private:
|
|
|
|
[[nodiscard]] bool constexpr isInitialized() const
|
|
|
|
{
|
2022-02-24 18:14:46 +00:00
|
|
|
return piece_size != 0;
|
2022-02-18 23:17:19 +00:00
|
|
|
}
|
2021-11-24 14:48:52 +00:00
|
|
|
};
|