2
This file is part of TON Blockchain Library.
4
TON Blockchain Library is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Lesser General Public License as published by
6
the Free Software Foundation, either version 2 of the License, or
7
(at your option) any later version.
9
TON Blockchain Library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU Lesser General Public License for more details.
14
You should have received a copy of the GNU Lesser General Public License
15
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
17
Copyright 2017-2020 Telegram Systems LLP
20
#include "TorrentInfo.h"
22
#include "vm/cells/CellString.h"
23
#include "vm/cellslice.h"
25
#include "td/utils/misc.h"
28
bool TorrentInfo::pack(vm::CellBuilder &cb) const {
29
return cb.store_long_bool(piece_size, 32) && cb.store_long_bool(file_size, 64) && cb.store_bits_bool(root_hash) &&
30
cb.store_long_bool(header_size, 64) && cb.store_bits_bool(header_hash) &&
31
vm::CellText::store(cb, description).is_ok();
34
bool TorrentInfo::unpack(vm::CellSlice &cs) {
35
return cs.fetch_uint_to(32, piece_size) && cs.fetch_uint_to(64, file_size) && cs.fetch_bits_to(root_hash) &&
36
cs.fetch_uint_to(64, header_size) && cs.fetch_bits_to(header_hash) && vm::CellText::fetch_to(cs, description);
39
td::Bits256 TorrentInfo::get_hash() const {
40
return as_cell()->get_hash().bits();
43
void TorrentInfo::init_cell() {
46
cell_ = cb.finalize();
49
td::Ref<vm::Cell> TorrentInfo::as_cell() const {
50
CHECK(cell_.not_null())
54
td::uint64 TorrentInfo::pieces_count() const {
55
return (file_size + piece_size - 1) / piece_size;
58
TorrentInfo::PieceInfo TorrentInfo::get_piece_info(td::uint64 piece_i) const {
60
info.offset = piece_size * piece_i;
61
CHECK(info.offset < file_size);
62
info.size = td::min(static_cast<td::uint64>(piece_size), file_size - info.offset);
66
td::Status TorrentInfo::validate() const {
67
if (piece_size == 0) {
68
return td::Status::Error("Piece size is 0");
70
if (header_size > file_size) {
71
return td::Status::Error("Header is too big");
73
if (description.size() > 1024) {
74
return td::Status::Error("Description is too long");
76
if (piece_size > (1 << 23)) {
77
return td::Status::Error("Piece size is too big");
79
if (pieces_count() >= (1ULL << 31)) {
80
return td::Status::Error("Too many pieces");
82
return td::Status::OK();