Ton

Форк
0
/
TorrentInfo.cpp 
84 строки · 2.7 Кб
1
/*
2
    This file is part of TON Blockchain Library.
3

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.
8

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.
13

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/>.
16

17
    Copyright 2017-2020 Telegram Systems LLP
18
*/
19

20
#include "TorrentInfo.h"
21

22
#include "vm/cells/CellString.h"
23
#include "vm/cellslice.h"
24

25
#include "td/utils/misc.h"
26

27
namespace ton {
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();
32
}
33

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);
37
}
38

39
td::Bits256 TorrentInfo::get_hash() const {
40
  return as_cell()->get_hash().bits();
41
}
42

43
void TorrentInfo::init_cell() {
44
  vm::CellBuilder cb;
45
  CHECK(pack(cb));
46
  cell_ = cb.finalize();
47
}
48

49
td::Ref<vm::Cell> TorrentInfo::as_cell() const {
50
  CHECK(cell_.not_null())
51
  return cell_;
52
}
53

54
td::uint64 TorrentInfo::pieces_count() const {
55
  return (file_size + piece_size - 1) / piece_size;
56
}
57

58
TorrentInfo::PieceInfo TorrentInfo::get_piece_info(td::uint64 piece_i) const {
59
  PieceInfo info;
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);
63
  return info;
64
}
65

66
td::Status TorrentInfo::validate() const {
67
  if (piece_size == 0) {
68
    return td::Status::Error("Piece size is 0");
69
  }
70
  if (header_size > file_size) {
71
    return td::Status::Error("Header is too big");
72
  }
73
  if (description.size() > 1024) {
74
    return td::Status::Error("Description is too long");
75
  }
76
  if (piece_size > (1 << 23)) {
77
    return td::Status::Error("Piece size is too big");
78
  }
79
  if (pieces_count() >= (1ULL << 31)) {
80
    return td::Status::Error("Too many pieces");
81
  }
82
  return td::Status::OK();
83
}
84
}  // namespace ton
85

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.