2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2021-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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
|
|
|
|
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <cstdint> // uint32_t, uint64_t
|
|
|
|
|
2021-11-24 14:48:52 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
struct tr_block_info
|
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
private:
|
|
|
|
uint64_t total_size_ = 0;
|
2022-04-15 05:38:59 +00:00
|
|
|
uint32_t piece_size_ = 0;
|
|
|
|
tr_piece_index_t n_pieces_ = 0;
|
2022-02-23 20:25:06 +00:00
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
tr_block_index_t n_blocks_ = 0;
|
2022-04-15 05:38:59 +00:00
|
|
|
// should be same type as BlockSize
|
2022-04-14 01:22:59 +00:00
|
|
|
uint32_t final_block_size_ = 0;
|
2022-04-15 05:38:59 +00:00
|
|
|
// should be same type as piece_size
|
2022-04-14 01:22:59 +00:00
|
|
|
uint32_t final_piece_size_ = 0;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
public:
|
|
|
|
static auto constexpr BlockSize = uint32_t{ 1024 * 16 };
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
tr_block_info() noexcept = default;
|
|
|
|
|
2022-04-15 05:38:59 +00:00
|
|
|
tr_block_info(uint64_t total_size_in, uint32_t piece_size_in) noexcept
|
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
|
|
|
}
|
|
|
|
|
2022-04-15 05:38:59 +00:00
|
|
|
void initSizes(uint64_t total_size_in, uint32_t piece_size_in) noexcept;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto blockCount() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return n_blocks_;
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
[[nodiscard]] constexpr auto blockSize(tr_block_index_t block) const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return block + 1 == n_blocks_ ? final_block_size_ : BlockSize;
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto pieceCount() const noexcept
|
2022-01-07 19:13:37 +00:00
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return n_pieces_;
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto pieceSize() const noexcept
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return piece_size_;
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] constexpr auto pieceSize(tr_piece_index_t piece) const noexcept
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return piece + 1 == n_pieces_ ? final_piece_size_ : pieceSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] constexpr auto totalSize() const noexcept
|
|
|
|
{
|
|
|
|
return total_size_;
|
2021-11-24 14:48:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
[[nodiscard]] tr_block_span_t blockSpanForPiece(tr_piece_index_t piece) const noexcept
|
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-08-02 23:34:53 +00:00
|
|
|
return { 0U, 0U };
|
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-07-04 16:48:54 +00:00
|
|
|
[[nodiscard]] tr_byte_span_t byteSpanForPiece(tr_piece_index_t piece) const noexcept
|
|
|
|
{
|
|
|
|
if (!isInitialized())
|
|
|
|
{
|
2022-08-02 23:34:53 +00:00
|
|
|
return { 0U, 0U };
|
2022-07-04 16:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto const offset = pieceLoc(piece).byte;
|
|
|
|
return { offset, offset + pieceSize(piece) };
|
|
|
|
}
|
|
|
|
|
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`.
|
2022-04-14 01:22:59 +00:00
|
|
|
[[nodiscard]] Location blockLoc(tr_block_index_t block) const noexcept;
|
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`
|
2022-04-14 01:22:59 +00:00
|
|
|
[[nodiscard]] Location pieceLoc(tr_piece_index_t piece, uint32_t offset = 0, uint32_t length = 0) const noexcept;
|
2022-02-23 13:38:18 +00:00
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
// Location of the torrent's nth byte
|
|
|
|
[[nodiscard]] Location byteLoc(uint64_t byte_idx) const noexcept;
|
2021-11-24 14:48:52 +00:00
|
|
|
|
2022-04-14 01:22:59 +00:00
|
|
|
private:
|
2022-02-18 23:17:19 +00:00
|
|
|
// Location of the last byte in `piece`.
|
2022-04-14 01:22:59 +00:00
|
|
|
[[nodiscard]] Location pieceLastLoc(tr_piece_index_t piece) const
|
2021-11-24 14:48:52 +00:00
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return byteLoc(static_cast<uint64_t>(piece) * pieceSize() + pieceSize(piece) - 1);
|
2022-01-07 19:13:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 00:48:09 +00:00
|
|
|
[[nodiscard]] bool constexpr isInitialized() const noexcept
|
2022-02-18 23:17:19 +00:00
|
|
|
{
|
2022-04-14 01:22:59 +00:00
|
|
|
return piece_size_ != 0;
|
2022-02-18 23:17:19 +00:00
|
|
|
}
|
2021-11-24 14:48:52 +00:00
|
|
|
};
|