Use explicit as_mutable_slice to get MutableSlice.

This commit is contained in:
levlam 2023-02-02 17:40:47 +03:00
parent d6b7770561
commit 0adcac246a
32 changed files with 127 additions and 130 deletions

View File

@ -306,14 +306,14 @@ class AesCbcDecryptBench final : public td::Benchmark {
void start_up() final { void start_up() final {
std::fill(std::begin(data), std::end(data), static_cast<unsigned char>(123)); std::fill(std::begin(data), std::end(data), static_cast<unsigned char>(123));
td::Random::secure_bytes(as_slice(key)); td::Random::secure_bytes(as_mutable_slice(key));
td::Random::secure_bytes(as_slice(iv)); td::Random::secure_bytes(as_mutable_slice(iv));
} }
void run(int n) final { void run(int n) final {
td::MutableSlice data_slice(data, DATA_SIZE); td::MutableSlice data_slice(data, DATA_SIZE);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
td::aes_cbc_decrypt(as_slice(key), as_slice(iv), data_slice, data_slice); td::aes_cbc_decrypt(as_slice(key), as_mutable_slice(iv), data_slice, data_slice);
} }
} }
}; };
@ -330,14 +330,14 @@ class AesCbcEncryptBench final : public td::Benchmark {
void start_up() final { void start_up() final {
std::fill(std::begin(data), std::end(data), static_cast<unsigned char>(123)); std::fill(std::begin(data), std::end(data), static_cast<unsigned char>(123));
td::Random::secure_bytes(as_slice(key)); td::Random::secure_bytes(as_mutable_slice(key));
td::Random::secure_bytes(as_slice(iv)); td::Random::secure_bytes(as_mutable_slice(iv));
} }
void run(int n) final { void run(int n) final {
td::MutableSlice data_slice(data, DATA_SIZE); td::MutableSlice data_slice(data, DATA_SIZE);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
td::aes_cbc_encrypt(as_slice(key), as_slice(iv), data_slice, data_slice); td::aes_cbc_encrypt(as_slice(key), as_mutable_slice(iv), data_slice, data_slice);
} }
} }
}; };
@ -355,8 +355,8 @@ class AesIgeShortBench final : public td::Benchmark {
void start_up() final { void start_up() final {
std::fill(std::begin(data), std::end(data), static_cast<unsigned char>(123)); std::fill(std::begin(data), std::end(data), static_cast<unsigned char>(123));
td::Random::secure_bytes(as_slice(key)); td::Random::secure_bytes(as_mutable_slice(key));
td::Random::secure_bytes(as_slice(iv)); td::Random::secure_bytes(as_mutable_slice(iv));
} }
void run(int n) final { void run(int n) final {
@ -367,7 +367,7 @@ class AesIgeShortBench final : public td::Benchmark {
ige.init(as_slice(key), as_slice(iv), false); ige.init(as_slice(key), as_slice(iv), false);
ige.decrypt(data_slice, data_slice); ige.decrypt(data_slice, data_slice);
} else { } else {
td::aes_ige_decrypt(as_slice(key), as_slice(iv), data_slice, data_slice); td::aes_ige_decrypt(as_slice(key), as_mutable_slice(iv), data_slice, data_slice);
} }
} }
} }

View File

@ -185,7 +185,7 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection
auto save_tmp_aes_iv = tmp_aes_iv; auto save_tmp_aes_iv = tmp_aes_iv;
// encrypted_answer := AES256_ige_encrypt (answer_with_hash, tmp_aes_key, tmp_aes_iv); // encrypted_answer := AES256_ige_encrypt (answer_with_hash, tmp_aes_key, tmp_aes_iv);
MutableSlice answer(const_cast<char *>(dh_params->encrypted_answer_.begin()), dh_params->encrypted_answer_.size()); MutableSlice answer(const_cast<char *>(dh_params->encrypted_answer_.begin()), dh_params->encrypted_answer_.size());
aes_ige_decrypt(as_slice(tmp_aes_key), as_slice(tmp_aes_iv), answer, answer); aes_ige_decrypt(as_slice(tmp_aes_key), as_mutable_slice(tmp_aes_iv), answer, answer);
tmp_aes_iv = save_tmp_aes_iv; tmp_aes_iv = save_tmp_aes_iv;
// answer_with_hash := SHA1(answer) + answer + (0-15 random bytes) // answer_with_hash := SHA1(answer) + answer + (0-15 random bytes)
@ -238,7 +238,7 @@ Status AuthKeyHandshake::on_server_dh_params(Slice message, Callback *connection
Random::secure_bytes(encrypted_data.ubegin() + encrypted_data_size, Random::secure_bytes(encrypted_data.ubegin() + encrypted_data_size,
encrypted_data_size_with_pad - encrypted_data_size); encrypted_data_size_with_pad - encrypted_data_size);
tmp_KDF(server_nonce_, new_nonce_, &tmp_aes_key, &tmp_aes_iv); tmp_KDF(server_nonce_, new_nonce_, &tmp_aes_key, &tmp_aes_iv);
aes_ige_encrypt(as_slice(tmp_aes_key), as_slice(tmp_aes_iv), encrypted_data, encrypted_data); aes_ige_encrypt(as_slice(tmp_aes_key), as_mutable_slice(tmp_aes_iv), encrypted_data, encrypted_data);
mtproto_api::set_client_DH_params set_client_dh_params(nonce_, server_nonce_, encrypted_data); mtproto_api::set_client_DH_params set_client_dh_params(nonce_, server_nonce_, encrypted_data);
send(connection, create_storer(set_client_dh_params)); send(connection, create_storer(set_client_dh_params));
@ -289,7 +289,7 @@ Status AuthKeyHandshake::on_dh_gen_response(Slice message, Callback *connection)
void AuthKeyHandshake::send(Callback *connection, const Storer &storer) { void AuthKeyHandshake::send(Callback *connection, const Storer &storer) {
auto size = storer.size(); auto size = storer.size();
auto writer = BufferWriter{size, 0, 0}; auto writer = BufferWriter{size, 0, 0};
auto real_size = storer.store(writer.as_slice().ubegin()); auto real_size = storer.store(writer.as_mutable_slice().ubegin());
CHECK(real_size == size); CHECK(real_size == size);
last_query_ = writer.as_buffer_slice(); last_query_ = writer.as_buffer_slice();
return do_send(connection, create_storer(last_query_.as_slice())); return do_send(connection, create_storer(last_query_.as_slice()));

View File

@ -65,7 +65,7 @@ class RawConnectionDefault final : public RawConnection {
auto packet = BufferWriter{Transport::write(storer, auth_key, &info), transport_->max_prepend_size(), auto packet = BufferWriter{Transport::write(storer, auth_key, &info), transport_->max_prepend_size(),
transport_->max_append_size()}; transport_->max_append_size()};
Transport::write(storer, auth_key, &info, packet.as_slice()); Transport::write(storer, auth_key, &info, packet.as_mutable_slice());
bool use_quick_ack = false; bool use_quick_ack = false;
if (quick_ack_token != 0 && transport_->support_quick_ack()) { if (quick_ack_token != 0 && transport_->support_quick_ack()) {
@ -89,7 +89,7 @@ class RawConnectionDefault final : public RawConnection {
info.no_crypto_flag = true; info.no_crypto_flag = true;
auto packet = BufferWriter{Transport::write(storer, AuthKey(), &info), transport_->max_prepend_size(), auto packet = BufferWriter{Transport::write(storer, AuthKey(), &info), transport_->max_prepend_size(),
transport_->max_append_size()}; transport_->max_append_size()};
Transport::write(storer, AuthKey(), &info, packet.as_slice()); Transport::write(storer, AuthKey(), &info, packet.as_mutable_slice());
LOG(INFO) << "Send handshake packet: " << format::as_hex_dump<4>(packet.as_slice()); LOG(INFO) << "Send handshake packet: " << format::as_hex_dump<4>(packet.as_slice());
transport_->write(std::move(packet), false); transport_->write(std::move(packet), false);
return info.message_id; return info.message_id;
@ -156,7 +156,7 @@ class RawConnectionDefault final : public RawConnection {
TRY_RESULT(wait_size, transport_->read_next(&packet, &quick_ack)); TRY_RESULT(wait_size, transport_->read_next(&packet, &quick_ack));
if (!is_aligned_pointer<4>(packet.as_slice().ubegin())) { if (!is_aligned_pointer<4>(packet.as_slice().ubegin())) {
BufferSlice new_packet(packet.size()); BufferSlice new_packet(packet.size());
new_packet.as_slice().copy_from(packet.as_slice()); new_packet.as_mutable_slice().copy_from(packet.as_slice());
packet = std::move(new_packet); packet = std::move(new_packet);
} }
LOG_CHECK(is_aligned_pointer<4>(packet.as_slice().ubegin())) LOG_CHECK(is_aligned_pointer<4>(packet.as_slice().ubegin()))
@ -177,7 +177,7 @@ class RawConnectionDefault final : public RawConnection {
PacketInfo info; PacketInfo info;
info.version = 2; info.version = 2;
TRY_RESULT(read_result, Transport::read(packet.as_slice(), auth_key, &info)); TRY_RESULT(read_result, Transport::read(packet.as_mutable_slice(), auth_key, &info));
switch (read_result.type()) { switch (read_result.type()) {
case Transport::ReadResult::Quickack: { case Transport::ReadResult::Quickack: {
TRY_STATUS(on_quick_ack(read_result.quick_ack(), callback)); TRY_STATUS(on_quick_ack(read_result.quick_ack(), callback));
@ -389,7 +389,7 @@ class RawConnectionHttp final : public RawConnection {
PacketInfo info; PacketInfo info;
info.version = 2; info.version = 2;
TRY_RESULT(read_result, Transport::read(packet.as_slice(), auth_key, &info)); TRY_RESULT(read_result, Transport::read(packet.as_mutable_slice(), auth_key, &info));
switch (read_result.type()) { switch (read_result.type()) {
case Transport::ReadResult::Quickack: { case Transport::ReadResult::Quickack: {
break; break;

View File

@ -818,7 +818,7 @@ std::pair<uint64, BufferSlice> SessionConnection::encrypted_bind(int64 perm_key,
auto object_storer = create_storer(object); auto object_storer = create_storer(object);
auto size = object_storer.size(); auto size = object_storer.size();
auto object_packet = BufferWriter{size, 0, 0}; auto object_packet = BufferWriter{size, 0, 0};
auto real_size = object_storer.store(object_packet.as_slice().ubegin()); auto real_size = object_storer.store(object_packet.as_mutable_slice().ubegin());
CHECK(size == real_size); CHECK(size == real_size);
MtprotoQuery query{ MtprotoQuery query{
@ -833,7 +833,7 @@ std::pair<uint64, BufferSlice> SessionConnection::encrypted_bind(int64 perm_key,
const AuthKey &main_auth_key = auth_data_->get_main_auth_key(); const AuthKey &main_auth_key = auth_data_->get_main_auth_key();
auto packet = BufferWriter{Transport::write(query_storer, main_auth_key, &info), 0, 0}; auto packet = BufferWriter{Transport::write(query_storer, main_auth_key, &info), 0, 0};
Transport::write(query_storer, main_auth_key, &info, packet.as_slice()); Transport::write(query_storer, main_auth_key, &info, packet.as_mutable_slice());
return std::make_pair(query.message_id, packet.as_buffer_slice()); return std::make_pair(query.message_id, packet.as_buffer_slice());
} }

View File

@ -69,7 +69,7 @@ void IntermediateTransport::write_prepare_inplace(BufferWriter *message, bool qu
message->confirm_append(append.size()); message->confirm_append(append.size());
} }
as<uint32>(message->as_slice().begin()) = static_cast<uint32>(size + append_size); as<uint32>(message->as_mutable_slice().begin()) = static_cast<uint32>(size + append_size);
} }
void IntermediateTransport::init_output_stream(ChainBufferWriter *stream) { void IntermediateTransport::init_output_stream(ChainBufferWriter *stream) {
@ -121,7 +121,7 @@ void ObfuscatedTransport::init(ChainBufferReader *input, ChainBufferWriter *outp
state.init(); state.init();
state.feed(as_slice(key)); state.feed(as_slice(key));
state.feed(proxy_secret); state.feed(proxy_secret);
state.extract(as_slice(key)); state.extract(as_mutable_slice(key));
} }
}; };
fix_key(key); fix_key(key);
@ -153,7 +153,7 @@ Result<size_t> ObfuscatedTransport::read_next(BufferSlice *message, uint32 *quic
void ObfuscatedTransport::write(BufferWriter &&message, bool quick_ack) { void ObfuscatedTransport::write(BufferWriter &&message, bool quick_ack) {
impl_.write_prepare_inplace(&message, quick_ack); impl_.write_prepare_inplace(&message, quick_ack);
output_state_.encrypt(message.as_slice(), message.as_slice()); output_state_.encrypt(message.as_slice(), message.as_mutable_slice());
if (secret_.emulate_tls()) { if (secret_.emulate_tls()) {
do_write_tls(std::move(message)); do_write_tls(std::move(message));
} else { } else {

View File

@ -490,7 +490,7 @@ Status TlsInit::wait_hello_response() {
} }
auto response = fd_.input_buffer().cut_head(it.begin().clone()).move_as_buffer_slice(); auto response = fd_.input_buffer().cut_head(it.begin().clone()).move_as_buffer_slice();
auto response_rand_slice = response.as_slice().substr(11, 32); auto response_rand_slice = response.as_mutable_slice().substr(11, 32);
auto response_rand = response_rand_slice.str(); auto response_rand = response_rand_slice.str();
std::fill(response_rand_slice.begin(), response_rand_slice.end(), '\0'); std::fill(response_rand_slice.begin(), response_rand_slice.end(), '\0');
std::string hash_dest(32, '\0'); std::string hash_dest(32, '\0');

View File

@ -158,7 +158,7 @@ std::pair<uint32, UInt128> Transport::calc_message_key2(const AuthKey &auth_key,
// msg_key = substr (msg_key_large, 8, 16); // msg_key = substr (msg_key_large, 8, 16);
UInt128 res; UInt128 res;
as_slice(res).copy_from(msg_key_large.substr(8, 16)); as_mutable_slice(res).copy_from(msg_key_large.substr(8, 16));
return std::make_pair(as<uint32>(msg_key_large_raw) | (1u << 31), res); return std::make_pair(as<uint32>(msg_key_large_raw) | (1u << 31), res);
} }
@ -243,7 +243,7 @@ Status Transport::read_crypto_impl(int X, MutableSlice message, const AuthKey &a
KDF2(auth_key.key(), header->message_key, X, &aes_key, &aes_iv); KDF2(auth_key.key(), header->message_key, X, &aes_key, &aes_iv);
} }
aes_ige_decrypt(as_slice(aes_key), as_slice(aes_iv), to_decrypt, to_decrypt); aes_ige_decrypt(as_slice(aes_key), as_mutable_slice(aes_iv), to_decrypt, to_decrypt);
size_t tail_size = message.end() - reinterpret_cast<char *>(header->data); size_t tail_size = message.end() - reinterpret_cast<char *>(header->data);
if (tail_size < sizeof(PrefixT)) { if (tail_size < sizeof(PrefixT)) {
@ -378,7 +378,7 @@ void Transport::write_crypto_impl(int X, const Storer &storer, const AuthKey &au
KDF2(auth_key.key(), header->message_key, X, &aes_key, &aes_iv); KDF2(auth_key.key(), header->message_key, X, &aes_key, &aes_iv);
} }
aes_ige_encrypt(as_slice(aes_key), as_slice(aes_iv), to_encrypt, to_encrypt); aes_ige_encrypt(as_slice(aes_key), as_mutable_slice(aes_iv), to_encrypt, to_encrypt);
} }
size_t Transport::write_crypto(const Storer &storer, const AuthKey &auth_key, PacketInfo *info, MutableSlice dest) { size_t Transport::write_crypto(const Storer &storer, const AuthKey &auth_key, PacketInfo *info, MutableSlice dest) {

View File

@ -181,9 +181,9 @@ Result<SimpleConfig> decode_config(Slice input) {
MutableSlice data_cbc = data_rsa_slice.substr(32); MutableSlice data_cbc = data_rsa_slice.substr(32);
UInt256 key; UInt256 key;
UInt128 iv; UInt128 iv;
as_slice(key).copy_from(data_rsa_slice.substr(0, 32)); as_mutable_slice(key).copy_from(data_rsa_slice.substr(0, 32));
as_slice(iv).copy_from(data_rsa_slice.substr(16, 16)); as_mutable_slice(iv).copy_from(data_rsa_slice.substr(16, 16));
aes_cbc_decrypt(as_slice(key), as_slice(iv), data_cbc, data_cbc); aes_cbc_decrypt(as_slice(key), as_mutable_slice(iv), data_cbc, data_cbc);
CHECK(data_cbc.size() == 224); CHECK(data_cbc.size() == 224);
string hash(32, ' '); string hash(32, ' ');

View File

@ -6273,7 +6273,7 @@ BufferSlice MessagesManager::get_dialog_database_value(const Dialog *d) {
store(*d, storer_calc_length); store(*d, storer_calc_length);
BufferSlice value_buffer{storer_calc_length.get_length()}; BufferSlice value_buffer{storer_calc_length.get_length()};
auto value = value_buffer.as_slice(); auto value = value_buffer.as_mutable_slice();
LogEventStorerUnsafe storer_unsafe(value.ubegin()); LogEventStorerUnsafe storer_unsafe(value.ubegin());
store(*d, storer_unsafe); store(*d, storer_unsafe);

View File

@ -44,11 +44,11 @@ static void hash_sha256(Slice data, Slice salt, MutableSlice dest) {
BufferSlice PasswordManager::calc_password_hash(Slice password, Slice client_salt, Slice server_salt) { BufferSlice PasswordManager::calc_password_hash(Slice password, Slice client_salt, Slice server_salt) {
LOG(INFO) << "Begin password hash calculation"; LOG(INFO) << "Begin password hash calculation";
BufferSlice buf(32); BufferSlice buf(32);
hash_sha256(password, client_salt, buf.as_slice()); hash_sha256(password, client_salt, buf.as_mutable_slice());
hash_sha256(buf.as_slice(), server_salt, buf.as_slice()); hash_sha256(buf.as_slice(), server_salt, buf.as_mutable_slice());
BufferSlice hash(64); BufferSlice hash(64);
pbkdf2_sha512(buf.as_slice(), client_salt, 100000, hash.as_slice()); pbkdf2_sha512(buf.as_slice(), client_salt, 100000, hash.as_mutable_slice());
hash_sha256(hash.as_slice(), server_salt, buf.as_slice()); hash_sha256(hash.as_slice(), server_salt, buf.as_mutable_slice());
LOG(INFO) << "End password hash calculation"; LOG(INFO) << "End password hash calculation";
return buf; return buf;
} }
@ -101,7 +101,7 @@ tl_object_ptr<telegram_api::InputCheckPasswordSRP> PasswordManager::get_input_ch
auto x_bn = BigNum::from_binary(x.as_slice()); auto x_bn = BigNum::from_binary(x.as_slice());
BufferSlice a(2048 / 8); BufferSlice a(2048 / 8);
Random::secure_bytes(a.as_slice()); Random::secure_bytes(a.as_mutable_slice());
auto a_bn = BigNum::from_binary(a.as_slice()); auto a_bn = BigNum::from_binary(a.as_slice());
BigNumContext ctx; BigNumContext ctx;
@ -636,8 +636,8 @@ void PasswordManager::update_password_settings(UpdateSettings update_settings, P
static BufferSlice create_salt(Slice salt_prefix) { static BufferSlice create_salt(Slice salt_prefix) {
static constexpr size_t ADDED_SALT_SIZE = 32; static constexpr size_t ADDED_SALT_SIZE = 32;
BufferSlice new_salt(salt_prefix.size() + ADDED_SALT_SIZE); BufferSlice new_salt(salt_prefix.size() + ADDED_SALT_SIZE);
new_salt.as_slice().copy_from(salt_prefix); new_salt.as_mutable_slice().copy_from(salt_prefix);
Random::secure_bytes(new_salt.as_slice().substr(salt_prefix.size())); Random::secure_bytes(new_salt.as_mutable_slice().substr(salt_prefix.size()));
return new_salt; return new_salt;
} }

View File

@ -216,7 +216,7 @@ Result<BufferSlice> SecretChatActor::create_encrypted_message(int32 my_in_seq_no
auto layer = current_layer(); auto layer = current_layer();
BufferSlice random_bytes(31); BufferSlice random_bytes(31);
Random::secure_bytes(random_bytes.as_slice().ubegin(), random_bytes.size()); Random::secure_bytes(random_bytes.as_mutable_slice().ubegin(), random_bytes.size());
auto message_with_layer = secret_api::make_object<secret_api::decryptedMessageLayer>( auto message_with_layer = secret_api::make_object<secret_api::decryptedMessageLayer>(
std::move(random_bytes), layer, in_seq_no, out_seq_no, std::move(message)); std::move(random_bytes), layer, in_seq_no, out_seq_no, std::move(message));
LOG(INFO) << "Create message " << to_string(message_with_layer); LOG(INFO) << "Create message " << to_string(message_with_layer);
@ -227,7 +227,7 @@ Result<BufferSlice> SecretChatActor::create_encrypted_message(int32 my_in_seq_no
info.version = 2; info.version = 2;
info.is_creator = auth_state_.x == 0; info.is_creator = auth_state_.x == 0;
auto packet_writer = BufferWriter{mtproto::Transport::write(new_storer, *auth_key, &info), 0, 0}; auto packet_writer = BufferWriter{mtproto::Transport::write(new_storer, *auth_key, &info), 0, 0};
mtproto::Transport::write(new_storer, *auth_key, &info, packet_writer.as_slice()); mtproto::Transport::write(new_storer, *auth_key, &info, packet_writer.as_mutable_slice());
message = std::move(message_with_layer->message_); message = std::move(message_with_layer->message_);
return packet_writer.as_buffer_slice(); return packet_writer.as_buffer_slice();
} }
@ -770,7 +770,7 @@ void SecretChatActor::tear_down() {
} }
Result<std::tuple<uint64, BufferSlice, int32>> SecretChatActor::decrypt(BufferSlice &encrypted_message) { Result<std::tuple<uint64, BufferSlice, int32>> SecretChatActor::decrypt(BufferSlice &encrypted_message) {
MutableSlice data = encrypted_message.as_slice(); MutableSlice data = encrypted_message.as_mutable_slice();
CHECK(is_aligned_pointer<4>(data.data())); CHECK(is_aligned_pointer<4>(data.data()));
TRY_RESULT(auth_key_id, mtproto::Transport::read_auth_key_id(data)); TRY_RESULT(auth_key_id, mtproto::Transport::read_auth_key_id(data));
mtproto::AuthKey *auth_key = nullptr; mtproto::AuthKey *auth_key = nullptr;
@ -789,7 +789,7 @@ Result<std::tuple<uint64, BufferSlice, int32>> SecretChatActor::decrypt(BufferSl
Result<mtproto::Transport::ReadResult> r_read_result; Result<mtproto::Transport::ReadResult> r_read_result;
for (size_t i = 0; i < versions.size(); i++) { for (size_t i = 0; i < versions.size(); i++) {
encrypted_message_copy = encrypted_message.copy(); encrypted_message_copy = encrypted_message.copy();
data = encrypted_message_copy.as_slice(); data = encrypted_message_copy.as_mutable_slice();
CHECK(is_aligned_pointer<4>(data.data())); CHECK(is_aligned_pointer<4>(data.data()));
mtproto::PacketInfo info; mtproto::PacketInfo info;
@ -1504,7 +1504,7 @@ Status SecretChatActor::outbound_rewrite_with_empty(uint64 state_id) {
} }
cancel_query(state->net_query_ref); cancel_query(state->net_query_ref);
MutableSlice data = state->message->encrypted_message.as_slice(); Slice data = state->message->encrypted_message.as_slice();
CHECK(is_aligned_pointer<4>(data.data())); CHECK(is_aligned_pointer<4>(data.data()));
// Rewrite with delete itself // Rewrite with delete itself

View File

@ -24,7 +24,7 @@ Result<ValueHash> ValueHash::create(Slice data) {
if (data.size() != ::td::as_slice(hash).size()) { if (data.size() != ::td::as_slice(hash).size()) {
return Status::Error(PSLICE() << "Wrong hash size " << data.size()); return Status::Error(PSLICE() << "Wrong hash size " << data.size());
} }
::td::as_slice(hash).copy_from(data); ::td::as_mutable_slice(hash).copy_from(data);
return ValueHash{hash}; return ValueHash{hash};
} }
@ -41,17 +41,15 @@ static AesCbcState calc_aes_cbc_state_hash(Slice hash) {
AesCbcState calc_aes_cbc_state_pbkdf2(Slice secret, Slice salt) { AesCbcState calc_aes_cbc_state_pbkdf2(Slice secret, Slice salt) {
LOG(INFO) << "Begin AES CBC state calculation"; LOG(INFO) << "Begin AES CBC state calculation";
UInt<512> hash; UInt<512> hash;
auto hash_slice = as_slice(hash); pbkdf2_sha512(secret, salt, 100000, as_mutable_slice(hash));
pbkdf2_sha512(secret, salt, 100000, hash_slice); return calc_aes_cbc_state_hash(as_slice(hash));
return calc_aes_cbc_state_hash(hash_slice);
} }
AesCbcState calc_aes_cbc_state_sha512(Slice seed) { AesCbcState calc_aes_cbc_state_sha512(Slice seed) {
LOG(INFO) << "Begin AES CBC state calculation"; LOG(INFO) << "Begin AES CBC state calculation";
UInt<512> hash; UInt<512> hash;
auto hash_slice = as_slice(hash); sha512(seed, as_mutable_slice(hash));
sha512(seed, hash_slice); return calc_aes_cbc_state_hash(as_slice(hash));
return calc_aes_cbc_state_hash(hash_slice);
} }
template <class F> template <class F>
@ -72,20 +70,20 @@ Result<ValueHash> calc_value_hash(const DataView &data_view) {
return Status::OK(); return Status::OK();
})); }));
UInt256 res; UInt256 res;
state.extract(as_slice(res)); state.extract(as_mutable_slice(res));
return ValueHash{res}; return ValueHash{res};
} }
ValueHash calc_value_hash(Slice data) { ValueHash calc_value_hash(Slice data) {
UInt256 res; UInt256 res;
sha256(data, as_slice(res)); sha256(data, as_mutable_slice(res));
return ValueHash{res}; return ValueHash{res};
} }
BufferSlice gen_random_prefix(int64 data_size) { BufferSlice gen_random_prefix(int64 data_size) {
BufferSlice buff(narrow_cast<size_t>(((32 + 15 + data_size) & -16) - data_size)); BufferSlice buff(narrow_cast<size_t>(((32 + 15 + data_size) & -16) - data_size));
Random::secure_bytes(buff.as_slice()); Random::secure_bytes(buff.as_mutable_slice());
buff.as_slice()[0] = narrow_cast<uint8>(buff.size()); buff.as_mutable_slice()[0] = narrow_cast<uint8>(buff.size());
CHECK((buff.size() + data_size) % 16 == 0); CHECK((buff.size() + data_size) % 16 == 0);
return buff; return buff;
} }
@ -111,7 +109,7 @@ int64 FileDataView::size() const {
Result<BufferSlice> FileDataView::pread(int64 offset, int64 size) const { Result<BufferSlice> FileDataView::pread(int64 offset, int64 size) const {
auto slice = BufferSlice(narrow_cast<size_t>(size)); auto slice = BufferSlice(narrow_cast<size_t>(size));
TRY_RESULT(actual_size, fd_.pread(slice.as_slice(), offset)); TRY_RESULT(actual_size, fd_.pread(slice.as_mutable_slice(), offset));
if (static_cast<int64>(actual_size) != size) { if (static_cast<int64>(actual_size) != size) {
return Status::Error("Not enough data in file"); return Status::Error("Not enough data in file");
} }
@ -166,8 +164,8 @@ Result<BufferSlice> ConcatDataView::pread(int64 offset, int64 size) const {
} }
BufferSlice res(a.size() + b.size()); BufferSlice res(a.size() + b.size());
res.as_slice().copy_from(a.as_slice()); res.as_mutable_slice().copy_from(a.as_slice());
res.as_slice().substr(a.size()).copy_from(b.as_slice()); res.as_mutable_slice().substr(a.size()).copy_from(b.as_slice());
return std::move(res); return std::move(res);
} }
@ -195,17 +193,17 @@ Result<Secret> Secret::create(Slice secret) {
return Status::Error(PSLICE() << "Wrong checksum " << checksum); return Status::Error(PSLICE() << "Wrong checksum " << checksum);
} }
UInt256 res; UInt256 res;
::td::as_slice(res).copy_from(secret); ::td::as_mutable_slice(res).copy_from(secret);
UInt256 secret_sha256; UInt256 secret_sha256;
sha256(secret, ::td::as_slice(secret_sha256)); sha256(secret, ::td::as_mutable_slice(secret_sha256));
int64 hash = as<int64>(secret_sha256.raw); int64 hash = as<int64>(secret_sha256.raw);
return Secret{res, hash}; return Secret{res, hash};
} }
Secret Secret::create_new() { Secret Secret::create_new() {
UInt256 secret; UInt256 secret;
auto secret_slice = ::td::as_slice(secret); auto secret_slice = ::td::as_mutable_slice(secret);
Random::secure_bytes(secret_slice); Random::secure_bytes(secret_slice);
auto checksum_diff = secret_checksum(secret_slice); auto checksum_diff = secret_checksum(secret_slice);
auto new_byte = static_cast<uint8>((static_cast<uint32>(secret_slice.ubegin()[0]) + checksum_diff) % 255); auto new_byte = static_cast<uint8>((static_cast<uint32>(secret_slice.ubegin()[0]) + checksum_diff) % 255);
@ -239,7 +237,7 @@ EncryptedSecret Secret::encrypt(Slice key, Slice salt, EnryptionAlgorithm algori
}(); }();
UInt256 res; UInt256 res;
aes_cbc_state.encrypt(as_slice(), ::td::as_slice(res)); aes_cbc_state.encrypt(as_slice(), ::td::as_mutable_slice(res));
return EncryptedSecret::create(::td::as_slice(res)).move_as_ok(); return EncryptedSecret::create(::td::as_slice(res)).move_as_ok();
} }
@ -251,7 +249,7 @@ Result<EncryptedSecret> EncryptedSecret::create(Slice encrypted_secret) {
return Status::Error("Wrong encrypted secret size"); return Status::Error("Wrong encrypted secret size");
} }
UInt256 res; UInt256 res;
::td::as_slice(res).copy_from(encrypted_secret); ::td::as_mutable_slice(res).copy_from(encrypted_secret);
return EncryptedSecret{res}; return EncryptedSecret{res};
} }
@ -269,7 +267,7 @@ Result<Secret> EncryptedSecret::decrypt(Slice key, Slice salt, EnryptionAlgorith
}(); }();
UInt256 res; UInt256 res;
aes_cbc_state.decrypt(::td::as_slice(encrypted_secret_), ::td::as_slice(res)); aes_cbc_state.decrypt(::td::as_slice(encrypted_secret_), ::td::as_mutable_slice(res));
return Secret::create(::td::as_slice(res)); return Secret::create(::td::as_slice(res));
} }
@ -291,7 +289,7 @@ Result<BufferSlice> Decryptor::append(BufferSlice data) {
if (data.size() % 16 != 0) { if (data.size() % 16 != 0) {
return Status::Error("Part size must be divisible by 16"); return Status::Error("Part size must be divisible by 16");
} }
aes_cbc_state_.decrypt(data.as_slice(), data.as_slice()); aes_cbc_state_.decrypt(data.as_slice(), data.as_mutable_slice());
sha256_state_.feed(data.as_slice()); sha256_state_.feed(data.as_slice());
if (!skipped_prefix_) { if (!skipped_prefix_) {
to_skip_ = data.as_slice().ubegin()[0]; to_skip_ = data.as_slice().ubegin()[0];
@ -314,7 +312,7 @@ Result<ValueHash> Decryptor::finish() {
} }
UInt256 res; UInt256 res;
sha256_state_.extract(as_slice(res), true); sha256_state_.extract(as_mutable_slice(res), true);
return ValueHash{res}; return ValueHash{res};
} }
@ -334,7 +332,7 @@ Result<BufferSlice> Encryptor::pread(int64 offset, int64 size) const {
return Status::Error("Part size must be divisible by 16"); return Status::Error("Part size must be divisible by 16");
} }
TRY_RESULT(part, data_view_.pread(offset, size)); TRY_RESULT(part, data_view_.pread(offset, size));
aes_cbc_state_.encrypt(part.as_slice(), part.as_slice()); aes_cbc_state_.encrypt(part.as_slice(), part.as_mutable_slice());
current_offset_ += size; current_offset_ += size;
return std::move(part); return std::move(part);
} }

View File

@ -5491,7 +5491,7 @@ string StickersManager::get_sticker_set_database_value(const StickerSet *s, bool
store_sticker_set(s, with_stickers, storer_calc_length, source); store_sticker_set(s, with_stickers, storer_calc_length, source);
BufferSlice value_buffer{storer_calc_length.get_length()}; BufferSlice value_buffer{storer_calc_length.get_length()};
auto value = value_buffer.as_slice(); auto value = value_buffer.as_mutable_slice();
LOG(DEBUG) << "Serialized size of " << s->id_ << " is " << value.size(); LOG(DEBUG) << "Serialized size of " << s->id_ << " is " << value.size();

View File

@ -5403,7 +5403,10 @@ class CliClient final : public Actor {
it->part_size = left_size; it->part_size = left_size;
} }
BufferSlice block(narrow_cast<size_t>(it->part_size)); BufferSlice block(narrow_cast<size_t>(it->part_size));
FileFd::open(it->source, FileFd::Flags::Read).move_as_ok().pread(block.as_slice(), it->local_size).ensure(); FileFd::open(it->source, FileFd::Flags::Read)
.move_as_ok()
.pread(block.as_mutable_slice(), it->local_size)
.ensure();
if (rand_bool()) { if (rand_bool()) {
auto open_flags = FileFd::Flags::Write | (it->local_size ? 0 : FileFd::Flags::Truncate | FileFd::Flags::Create); auto open_flags = FileFd::Flags::Write | (it->local_size ? 0 : FileFd::Flags::Truncate | FileFd::Flags::Create);
FileFd::open(it->destination, open_flags).move_as_ok().pwrite(block.as_slice(), it->local_size).ensure(); FileFd::open(it->destination, open_flags).move_as_ok().pwrite(block.as_slice(), it->local_size).ensure();

View File

@ -51,7 +51,7 @@ class FileDbInterface {
object.as_key().store(calc_length); object.as_key().store(calc_length);
BufferSlice key_buffer{calc_length.get_length()}; BufferSlice key_buffer{calc_length.get_length()};
auto key = key_buffer.as_slice(); auto key = key_buffer.as_mutable_slice();
TlStorerUnsafe storer(key.ubegin()); TlStorerUnsafe storer(key.ubegin());
storer.store_int(LocationT::KEY_MAGIC); storer.store_int(LocationT::KEY_MAGIC);
object.as_key().store(storer); object.as_key().store(storer);

View File

@ -351,7 +351,7 @@ Result<size_t> FileDownloader::process_part(Part part, NetQueryPtr net_query) {
string iv = cdn_encryption_iv_; string iv = cdn_encryption_iv_;
as<uint32>(&iv[12]) = offset; as<uint32>(&iv[12]) = offset;
ctr_state.init(cdn_encryption_key_, iv); ctr_state.init(cdn_encryption_key_, iv);
ctr_state.decrypt(bytes.as_slice(), bytes.as_slice()); ctr_state.decrypt(bytes.as_slice(), bytes.as_mutable_slice());
} }
if (encryption_key_.is_secret()) { if (encryption_key_.is_secret()) {
LOG_CHECK(next_part_ == part.id) << tag("expected part.id", next_part_) << "!=" << tag("part.id", part.id); LOG_CHECK(next_part_ == part.id) << tag("expected part.id", next_part_) << "!=" << tag("part.id", part.id);
@ -360,8 +360,8 @@ Result<size_t> FileDownloader::process_part(Part part, NetQueryPtr net_query) {
if (part.size % 16 != 0) { if (part.size % 16 != 0) {
next_part_stop_ = true; next_part_stop_ = true;
} }
aes_ige_decrypt(as_slice(encryption_key_.key()), as_slice(encryption_key_.mutable_iv()), bytes.as_slice(), aes_ige_decrypt(as_slice(encryption_key_.key()), as_mutable_slice(encryption_key_.mutable_iv()), bytes.as_slice(),
bytes.as_slice()); bytes.as_mutable_slice());
} }
auto slice = bytes.as_slice().substr(0, part.size); auto slice = bytes.as_slice().substr(0, part.size);
@ -445,7 +445,7 @@ Result<FileLoader::CheckInfo> FileDownloader::check_loop(int64 checked_prefix_si
auto size = narrow_cast<size_t>(end_offset - begin_offset); auto size = narrow_cast<size_t>(end_offset - begin_offset);
auto slice = BufferSlice(size); auto slice = BufferSlice(size);
TRY_STATUS(acquire_fd()); TRY_STATUS(acquire_fd());
TRY_RESULT(read_size, fd_.pread(slice.as_slice(), begin_offset)); TRY_RESULT(read_size, fd_.pread(slice.as_mutable_slice(), begin_offset));
if (size != read_size) { if (size != read_size) {
return Status::Error("Failed to read file to check hash"); return Status::Error("Failed to read file to check hash");
} }

View File

@ -68,7 +68,7 @@ Status FileHashUploader::loop_impl() {
if (state_ == State::NetRequest) { if (state_ == State::NetRequest) {
// messages.getDocumentByHash#338e2464 sha256:bytes size:long mime_type:string = Document; // messages.getDocumentByHash#338e2464 sha256:bytes size:long mime_type:string = Document;
auto hash = BufferSlice(32); auto hash = BufferSlice(32);
sha256_state_.extract(hash.as_slice(), true); sha256_state_.extract(hash.as_mutable_slice(), true);
auto mime_type = MimeType::from_extension(PathView(local_.path_).extension(), "image/gif"); auto mime_type = MimeType::from_extension(PathView(local_.path_).extension(), "image/gif");
auto query = telegram_api::messages_getDocumentByHash(std::move(hash), size_, std::move(mime_type)); auto query = telegram_api::messages_getDocumentByHash(std::move(hash), size_, std::move(mime_type));
LOG(INFO) << "Send getDocumentByHash request: " << to_string(query); LOG(INFO) << "Send getDocumentByHash request: " << to_string(query);

View File

@ -224,12 +224,12 @@ Status FileUploader::generate_iv_map() {
CHECK(!fd_.empty()); CHECK(!fd_.empty());
for (; generate_offset_ + static_cast<int64>(part_size) < local_size_; for (; generate_offset_ + static_cast<int64>(part_size) < local_size_;
generate_offset_ += static_cast<int64>(part_size)) { generate_offset_ += static_cast<int64>(part_size)) {
TRY_RESULT(read_size, fd_.pread(bytes.as_slice(), generate_offset_)); TRY_RESULT(read_size, fd_.pread(bytes.as_mutable_slice(), generate_offset_));
if (read_size != part_size) { if (read_size != part_size) {
return Status::Error("Failed to read file part (for iv_map)"); return Status::Error("Failed to read file part (for iv_map)");
} }
aes_ige_encrypt(as_slice(encryption_key.key()), as_slice(encryption_key.mutable_iv()), bytes.as_slice(), aes_ige_encrypt(as_slice(encryption_key.key()), as_mutable_slice(encryption_key.mutable_iv()), bytes.as_slice(),
bytes.as_slice()); bytes.as_mutable_slice());
iv_map_.push_back(encryption_key.mutable_iv()); iv_map_.push_back(encryption_key.mutable_iv());
} }
generate_iv_ = encryption_key.iv_slice().str(); generate_iv_ = encryption_key.iv_slice().str();
@ -254,11 +254,12 @@ Result<std::pair<NetQueryPtr, bool>> FileUploader::start_part(Part part, int32 p
padded_size = (padded_size + 15) & ~15; padded_size = (padded_size + 15) & ~15;
} }
BufferSlice bytes(padded_size); BufferSlice bytes(padded_size);
TRY_RESULT(size, fd_.pread(bytes.as_slice().truncate(part.size), part.offset)); TRY_RESULT(size, fd_.pread(bytes.as_mutable_slice().truncate(part.size), part.offset));
if (encryption_key_.is_secret()) { if (encryption_key_.is_secret()) {
Random::secure_bytes(bytes.as_slice().substr(part.size)); Random::secure_bytes(bytes.as_mutable_slice().substr(part.size));
if (next_offset_ == part.offset) { if (next_offset_ == part.offset) {
aes_ige_encrypt(as_slice(encryption_key_.key()), as_slice(iv_), bytes.as_slice(), bytes.as_slice()); aes_ige_encrypt(as_slice(encryption_key_.key()), as_mutable_slice(iv_), bytes.as_slice(),
bytes.as_mutable_slice());
next_offset_ += static_cast<int64>(bytes.size()); next_offset_ += static_cast<int64>(bytes.size());
} else { } else {
if (part.id >= static_cast<int32>(iv_map_.size())) { if (part.id >= static_cast<int32>(iv_map_.size())) {
@ -266,7 +267,8 @@ Result<std::pair<NetQueryPtr, bool>> FileUploader::start_part(Part part, int32 p
} }
CHECK(part.id < static_cast<int32>(iv_map_.size()) && part.id >= 0); CHECK(part.id < static_cast<int32>(iv_map_.size()) && part.id >= 0);
auto iv = iv_map_[part.id]; auto iv = iv_map_[part.id];
aes_ige_encrypt(as_slice(encryption_key_.key()), as_slice(iv), bytes.as_slice(), bytes.as_slice()); aes_ige_encrypt(as_slice(encryption_key_.key()), as_mutable_slice(iv), bytes.as_slice(),
bytes.as_mutable_slice());
} }
} }

View File

@ -206,7 +206,7 @@ BufferSlice log_event_store_impl(const T &data, const char *file, int line) {
store(data, storer_calc_length); store(data, storer_calc_length);
BufferSlice value_buffer{storer_calc_length.get_length()}; BufferSlice value_buffer{storer_calc_length.get_length()};
auto ptr = value_buffer.as_slice().ubegin(); auto ptr = value_buffer.as_mutable_slice().ubegin();
LOG_CHECK(is_aligned_pointer<4>(ptr)) << ptr; LOG_CHECK(is_aligned_pointer<4>(ptr)) << ptr;
LogEventStorerUnsafe storer_unsafe(ptr); LogEventStorerUnsafe storer_unsafe(ptr);

View File

@ -35,7 +35,7 @@ NetQueryPtr NetQueryCreator::create(uint64 id, const telegram_api::Function &fun
LOG(INFO) << "Create query " << to_string(function); LOG(INFO) << "Create query " << to_string(function);
auto storer = DefaultStorer<telegram_api::Function>(function); auto storer = DefaultStorer<telegram_api::Function>(function);
BufferSlice slice(storer.size()); BufferSlice slice(storer.size());
auto real_size = storer.store(slice.as_slice().ubegin()); auto real_size = storer.store(slice.as_mutable_slice().ubegin());
LOG_CHECK(real_size == slice.size()) << real_size << " " << slice.size() << " " LOG_CHECK(real_size == slice.size()) << real_size << " " << slice.size() << " "
<< format::as_hex_dump<4>(Slice(slice.as_slice())); << format::as_hex_dump<4>(Slice(slice.as_slice()));

View File

@ -702,7 +702,7 @@ void Session::on_session_created(uint64 unique_id, uint64 first_message_id) {
if (is_main_) { if (is_main_) {
LOG(DEBUG) << "Sending updatesTooLong to force getDifference"; LOG(DEBUG) << "Sending updatesTooLong to force getDifference";
BufferSlice packet(4); BufferSlice packet(4);
as<int32>(packet.as_slice().begin()) = telegram_api::updatesTooLong::ID; as<int32>(packet.as_mutable_slice().begin()) = telegram_api::updatesTooLong::ID;
last_activity_timestamp_ = Time::now(); last_activity_timestamp_ = Time::now();
callback_->on_update(std::move(packet), auth_data_.get_auth_key().id()); callback_->on_update(std::move(packet), auth_data_.get_auth_key().id());
} }

View File

@ -581,9 +581,9 @@ Status Binlog::load_binlog(const Callback &callback, const Callback &debug_callb
} }
void Binlog::update_encryption(Slice key, Slice iv) { void Binlog::update_encryption(Slice key, Slice iv) {
as_slice(aes_ctr_key_).copy_from(key); as_mutable_slice(aes_ctr_key_).copy_from(key);
UInt128 aes_ctr_iv; UInt128 aes_ctr_iv;
as_slice(aes_ctr_iv).copy_from(iv); as_mutable_slice(aes_ctr_iv).copy_from(iv);
aes_ctr_state_.init(as_slice(aes_ctr_key_), as_slice(aes_ctr_iv)); aes_ctr_state_.init(as_slice(aes_ctr_key_), as_slice(aes_ctr_iv));
} }

View File

@ -55,7 +55,7 @@ Status BinlogEvent::validate() const {
BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) { BufferSlice BinlogEvent::create_raw(uint64 id, int32 type, int32 flags, const Storer &storer) {
auto raw_event = BufferSlice{storer.size() + MIN_SIZE}; auto raw_event = BufferSlice{storer.size() + MIN_SIZE};
TlStorerUnsafe tl_storer(raw_event.as_slice().ubegin()); TlStorerUnsafe tl_storer(raw_event.as_mutable_slice().ubegin());
tl_storer.store_int(narrow_cast<int32>(raw_event.size())); tl_storer.store_int(narrow_cast<int32>(raw_event.size()));
tl_storer.store_long(id); tl_storer.store_long(id);
tl_storer.store_int(type); tl_storer.store_int(type);

View File

@ -100,7 +100,7 @@ struct BinlogEvent {
static BufferSlice create_raw(uint64 id, int32 type, int32 flags, const Storer &storer); static BufferSlice create_raw(uint64 id, int32 type, int32 flags, const Storer &storer);
std::string public_to_string() const { string public_to_string() const {
return PSTRING() << "LogEvent[" << tag("id", format::as_hex(id_)) << tag("type", type_) << tag("flags", flags_) return PSTRING() << "LogEvent[" << tag("id", format::as_hex(id_)) << tag("type", type_) << tag("flags", flags_)
<< tag("data", get_data().size()) << "]" << debug_info_; << tag("data", get_data().size()) << "]" << debug_info_;
} }

View File

@ -162,7 +162,7 @@ Result<size_t> HttpReader::read_next(HttpQuery *query, bool can_be_slow) {
if (flow_sink_.is_ready()) { if (flow_sink_.is_ready()) {
CHECK(query_->container_.size() == 1u); CHECK(query_->container_.size() == 1u);
query_->container_.emplace_back(content_->cut_head(content_->size()).move_as_buffer_slice()); query_->container_.emplace_back(content_->cut_head(content_->size()).move_as_buffer_slice());
query_->content_ = query_->container_.back().as_slice(); query_->content_ = query_->container_.back().as_mutable_slice();
break; break;
} }
@ -207,9 +207,9 @@ Result<size_t> HttpReader::read_next(HttpQuery *query, bool can_be_slow) {
query_->container_.emplace_back(content_->cut_head(size).move_as_buffer_slice()); query_->container_.emplace_back(content_->cut_head(size).move_as_buffer_slice());
Status result; Status result;
if (content_type_lowercased_.find("application/x-www-form-urlencoded") != string::npos) { if (content_type_lowercased_.find("application/x-www-form-urlencoded") != string::npos) {
result = parse_parameters(query_->container_.back().as_slice()); result = parse_parameters(query_->container_.back().as_mutable_slice());
} else { } else {
result = parse_json_parameters(query_->container_.back().as_slice()); result = parse_json_parameters(query_->container_.back().as_mutable_slice());
} }
if (result.is_error()) { if (result.is_error()) {
if (result.code() == 413) { if (result.code() == 413) {
@ -293,7 +293,7 @@ Result<bool> HttpReader::parse_multipart_form_data(bool can_be_slow) {
CHECK(temp_file_.empty()); CHECK(temp_file_.empty());
temp_file_name_.clear(); temp_file_name_.clear();
Parser headers_parser(headers.as_slice()); Parser headers_parser(headers.as_mutable_slice());
while (headers_parser.status().is_ok() && !headers_parser.data().empty()) { while (headers_parser.status().is_ok() && !headers_parser.data().empty()) {
MutableSlice header_name = headers_parser.read_till(':'); MutableSlice header_name = headers_parser.read_till(':');
headers_parser.skip(':'); headers_parser.skip(':');
@ -432,7 +432,7 @@ Result<bool> HttpReader::parse_multipart_form_data(bool can_be_slow) {
} }
query_->container_.emplace_back(content_->cut_head(form_data_read_length_).move_as_buffer_slice()); query_->container_.emplace_back(content_->cut_head(form_data_read_length_).move_as_buffer_slice());
MutableSlice value = query_->container_.back().as_slice(); MutableSlice value = query_->container_.back().as_mutable_slice();
content_->advance(boundary_.size()); content_->advance(boundary_.size());
form_data_skipped_length_ += form_data_read_length_ + boundary_.size(); form_data_skipped_length_ += form_data_read_length_ + boundary_.size();
form_data_read_length_ = 0; form_data_read_length_ = 0;
@ -538,7 +538,7 @@ Result<size_t> HttpReader::split_header() {
CHECK(query_->container_.back().size() == headers_read_length_ + 2); CHECK(query_->container_.back().size() == headers_read_length_ + 2);
input_->advance(2); input_->advance(2);
total_headers_length_ = headers_read_length_; total_headers_length_ = headers_read_length_;
auto status = parse_head(query_->container_.back().as_slice()); auto status = parse_head(query_->container_.back().as_mutable_slice());
if (status.is_error()) { if (status.is_error()) {
return std::move(status); return std::move(status);
} }
@ -641,7 +641,7 @@ Status HttpReader::parse_json_parameters(MutableSlice parameters) {
return Status::Error(400, "Bad Request: extra data after string"); return Status::Error(400, "Bad Request: extra data after string");
} }
query_->container_.emplace_back("content"); query_->container_.emplace_back("content");
query_->args_.emplace_back(query_->container_.back().as_slice(), r_value.move_as_ok()); query_->args_.emplace_back(query_->container_.back().as_mutable_slice(), r_value.move_as_ok());
return Status::OK(); return Status::OK();
} }
parser.skip('{'); parser.skip('{');

View File

@ -51,7 +51,7 @@ class UdpReaderHelper {
buffer_ = BufferSlice(RESERVED_SIZE); buffer_ = BufferSlice(RESERVED_SIZE);
} }
CHECK(buffer_.size() >= MAX_PACKET_SIZE); CHECK(buffer_.size() >= MAX_PACKET_SIZE);
message.data = buffer_.as_slice().substr(0, MAX_PACKET_SIZE); message.data = buffer_.as_mutable_slice().substr(0, MAX_PACKET_SIZE);
} }
UdpMessage extract_udp_message(UdpSocketFd::InboundMessage &message) { UdpMessage extract_udp_message(UdpSocketFd::InboundMessage &message) {

View File

@ -304,7 +304,7 @@ inline Slice as_slice(Slice slice) {
return slice; return slice;
} }
inline MutableSlice as_slice(MutableSlice slice) { inline Slice as_slice(MutableSlice slice) {
return slice; return slice;
} }
@ -312,10 +312,6 @@ inline Slice as_slice(const string &str) {
return str; return str;
} }
inline MutableSlice as_slice(string &str) {
return str;
}
inline MutableSlice as_mutable_slice(MutableSlice slice) { inline MutableSlice as_mutable_slice(MutableSlice slice) {
return slice; return slice;
} }

View File

@ -20,7 +20,7 @@ struct UInt {
return Slice(raw, size / 8); return Slice(raw, size / 8);
} }
MutableSlice as_slice() { MutableSlice as_mutable_slice() {
return MutableSlice(raw, size / 8); return MutableSlice(raw, size / 8);
} }
@ -76,8 +76,8 @@ Slice as_slice(const UInt<size> &value) {
} }
template <size_t size> template <size_t size>
MutableSlice as_slice(UInt<size> &value) { MutableSlice as_mutable_slice(UInt<size> &value) {
return value.as_slice(); return value.as_mutable_slice();
} }
template <size_t size> template <size_t size>

View File

@ -141,7 +141,7 @@ class BufferSlice {
} }
explicit BufferSlice(Slice slice) : BufferSlice(slice.size()) { explicit BufferSlice(Slice slice) : BufferSlice(slice.size()) {
as_slice().copy_from(slice); as_mutable_slice().copy_from(slice);
} }
BufferSlice(const char *ptr, size_t size) : BufferSlice(Slice(ptr, size)) { BufferSlice(const char *ptr, size_t size) : BufferSlice(Slice(ptr, size)) {
@ -183,7 +183,7 @@ class BufferSlice {
return as_slice(); return as_slice();
} }
MutableSlice as_slice() { MutableSlice as_mutable_slice() {
if (is_null()) { if (is_null()) {
return MutableSlice(); return MutableSlice();
} }
@ -230,7 +230,7 @@ class BufferSlice {
// like in std::string // like in std::string
char *data() { char *data() {
return as_slice().data(); return as_mutable_slice().data();
} }
const char *data() const { const char *data() const {
return as_slice().data(); return as_slice().data();
@ -305,7 +305,7 @@ class BufferWriter {
} }
BufferWriter(Slice slice, size_t prepend, size_t append) BufferWriter(Slice slice, size_t prepend, size_t append)
: BufferWriter(BufferAllocator::create_writer(slice.size(), prepend, append)) { : BufferWriter(BufferAllocator::create_writer(slice.size(), prepend, append)) {
as_slice().copy_from(slice); as_mutable_slice().copy_from(slice);
} }
explicit BufferWriter(BufferWriterPtr buffer_ptr) : buffer_(std::move(buffer_ptr)) { explicit BufferWriter(BufferWriterPtr buffer_ptr) : buffer_(std::move(buffer_ptr)) {
} }
@ -325,7 +325,7 @@ class BufferWriter {
} }
return buffer_->end_.load(std::memory_order_relaxed) - buffer_->begin_; return buffer_->end_.load(std::memory_order_relaxed) - buffer_->begin_;
} }
MutableSlice as_slice() { MutableSlice as_mutable_slice() {
auto end = buffer_->end_.load(std::memory_order_relaxed); auto end = buffer_->end_.load(std::memory_order_relaxed);
return MutableSlice(buffer_->data_ + buffer_->begin_, buffer_->data_ + end); return MutableSlice(buffer_->data_ + buffer_->begin_, buffer_->data_ + end);
} }
@ -657,7 +657,7 @@ class ChainBufferReader {
} else { } else {
auto save_size = size(); auto save_size = size();
res = BufferSlice{save_size}; res = BufferSlice{save_size};
advance(save_size, res.as_slice()); advance(save_size, res.as_mutable_slice());
} }
*this = ChainBufferReader(); *this = ChainBufferReader();
return res; return res;
@ -828,11 +828,9 @@ class BufferBuilder {
inline Slice as_slice(const BufferSlice &value) { inline Slice as_slice(const BufferSlice &value) {
return value.as_slice(); return value.as_slice();
} }
inline MutableSlice as_slice(BufferSlice &value) {
return value.as_slice();
}
inline MutableSlice as_mutable_slice(BufferSlice &value) { inline MutableSlice as_mutable_slice(BufferSlice &value) {
return value.as_slice(); return value.as_mutable_slice();
} }
} // namespace td } // namespace td

View File

@ -1043,7 +1043,7 @@ Result<BufferSlice> rsa_encrypt_pkcs1_oaep(Slice public_key, Slice data) {
int outlen = RSA_size(rsa); int outlen = RSA_size(rsa);
BufferSlice res(outlen); BufferSlice res(outlen);
if (RSA_public_encrypt(narrow_cast<int>(data.size()), const_cast<unsigned char *>(data.ubegin()), if (RSA_public_encrypt(narrow_cast<int>(data.size()), const_cast<unsigned char *>(data.ubegin()),
res.as_slice().ubegin(), rsa, RSA_PKCS1_OAEP_PADDING) != outlen) { res.as_mutable_slice().ubegin(), rsa, RSA_PKCS1_OAEP_PADDING) != outlen) {
#else #else
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, nullptr); EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(pkey, nullptr);
if (!ctx) { if (!ctx) {
@ -1065,7 +1065,7 @@ Result<BufferSlice> rsa_encrypt_pkcs1_oaep(Slice public_key, Slice data) {
return Status::Error("Cannot calculate encrypted length"); return Status::Error("Cannot calculate encrypted length");
} }
BufferSlice res(outlen); BufferSlice res(outlen);
if (EVP_PKEY_encrypt(ctx, res.as_slice().ubegin(), &outlen, data.ubegin(), data.size()) <= 0) { if (EVP_PKEY_encrypt(ctx, res.as_mutable_slice().ubegin(), &outlen, data.ubegin(), data.size()) <= 0) {
#endif #endif
return Status::Error("Cannot encrypt"); return Status::Error("Cannot encrypt");
} }
@ -1095,7 +1095,7 @@ Result<BufferSlice> rsa_decrypt_pkcs1_oaep(Slice private_key, Slice data) {
size_t outlen = RSA_size(rsa); size_t outlen = RSA_size(rsa);
BufferSlice res(outlen); BufferSlice res(outlen);
auto inlen = RSA_private_decrypt(narrow_cast<int>(data.size()), const_cast<unsigned char *>(data.ubegin()), auto inlen = RSA_private_decrypt(narrow_cast<int>(data.size()), const_cast<unsigned char *>(data.ubegin()),
res.as_slice().ubegin(), rsa, RSA_PKCS1_OAEP_PADDING); res.as_mutable_slice().ubegin(), rsa, RSA_PKCS1_OAEP_PADDING);
if (inlen == -1) { if (inlen == -1) {
return Status::Error("Cannot decrypt"); return Status::Error("Cannot decrypt");
} }
@ -1121,7 +1121,7 @@ Result<BufferSlice> rsa_decrypt_pkcs1_oaep(Slice private_key, Slice data) {
return Status::Error("Cannot calculate decrypted length"); return Status::Error("Cannot calculate decrypted length");
} }
BufferSlice res(outlen); BufferSlice res(outlen);
if (EVP_PKEY_decrypt(ctx, res.as_slice().ubegin(), &outlen, data.ubegin(), data.size()) <= 0) { if (EVP_PKEY_decrypt(ctx, res.as_mutable_slice().ubegin(), &outlen, data.ubegin(), data.size()) <= 0) {
return Status::Error("Cannot decrypt"); return Status::Error("Cannot decrypt");
} }
#endif #endif

View File

@ -23,7 +23,7 @@ static td::vector<td::string> strings{"", "1", "short test string", td::string(1
TEST(Crypto, Aes) { TEST(Crypto, Aes) {
td::Random::Xorshift128plus rnd(123); td::Random::Xorshift128plus rnd(123);
td::UInt256 key; td::UInt256 key;
rnd.bytes(as_slice(key)); rnd.bytes(as_mutable_slice(key));
td::string plaintext(16, '\0'); td::string plaintext(16, '\0');
td::string encrypted(16, '\0'); td::string encrypted(16, '\0');
td::string decrypted(16, '\0'); td::string decrypted(16, '\0');
@ -34,8 +34,8 @@ TEST(Crypto, Aes) {
td::AesState decryptor; td::AesState decryptor;
decryptor.init(as_slice(key), false); decryptor.init(as_slice(key), false);
encryptor.encrypt(td::as_slice(plaintext).ubegin(), td::as_slice(encrypted).ubegin(), 16); encryptor.encrypt(td::as_slice(plaintext).ubegin(), td::as_mutable_slice(encrypted).ubegin(), 16);
decryptor.decrypt(td::as_slice(encrypted).ubegin(), td::as_slice(decrypted).ubegin(), 16); decryptor.decrypt(td::as_slice(encrypted).ubegin(), td::as_mutable_slice(decrypted).ubegin(), 16);
CHECK(decrypted == plaintext); CHECK(decrypted == plaintext);
CHECK(decrypted != encrypted); CHECK(decrypted != encrypted);
@ -135,7 +135,7 @@ TEST(Crypto, AesIgeState) {
for (const auto &str : td::rand_split(td::string(length / 16, '\0'))) { for (const auto &str : td::rand_split(td::string(length / 16, '\0'))) {
auto len = 16 * str.size(); auto len = 16 * str.size();
state.encrypt(td::Slice(s).substr(pos, len), td::MutableSlice(t).substr(pos, len)); state.encrypt(td::Slice(s).substr(pos, len), td::MutableSlice(t).substr(pos, len));
td::aes_ige_encrypt(as_slice(key), as_slice(iv_copy), td::Slice(s).substr(pos, len), td::aes_ige_encrypt(as_slice(key), as_mutable_slice(iv_copy), td::Slice(s).substr(pos, len),
td::MutableSlice(u).substr(pos, len)); td::MutableSlice(u).substr(pos, len));
pos += len; pos += len;
} }
@ -149,7 +149,7 @@ TEST(Crypto, AesIgeState) {
for (const auto &str : td::rand_split(td::string(length / 16, '\0'))) { for (const auto &str : td::rand_split(td::string(length / 16, '\0'))) {
auto len = 16 * str.size(); auto len = 16 * str.size();
state.decrypt(td::Slice(t).substr(pos, len), td::MutableSlice(t).substr(pos, len)); state.decrypt(td::Slice(t).substr(pos, len), td::MutableSlice(t).substr(pos, len));
td::aes_ige_decrypt(as_slice(key), as_slice(iv_copy), td::Slice(u).substr(pos, len), td::aes_ige_decrypt(as_slice(key), as_mutable_slice(iv_copy), td::Slice(u).substr(pos, len),
td::MutableSlice(u).substr(pos, len)); td::MutableSlice(u).substr(pos, len));
pos += len; pos += len;
} }
@ -191,7 +191,7 @@ TEST(Crypto, AesCbcState) {
for (const auto &str : td::rand_split(td::string(length / 16, '\0'))) { for (const auto &str : td::rand_split(td::string(length / 16, '\0'))) {
auto len = 16 * str.size(); auto len = 16 * str.size();
state.encrypt(td::Slice(s).substr(pos, len), td::MutableSlice(t).substr(pos, len)); state.encrypt(td::Slice(s).substr(pos, len), td::MutableSlice(t).substr(pos, len));
td::aes_cbc_encrypt(as_slice(key), as_slice(iv_copy), td::Slice(s).substr(pos, len), td::aes_cbc_encrypt(as_slice(key), as_mutable_slice(iv_copy), td::Slice(s).substr(pos, len),
td::MutableSlice(u).substr(pos, len)); td::MutableSlice(u).substr(pos, len));
pos += len; pos += len;
} }
@ -205,7 +205,7 @@ TEST(Crypto, AesCbcState) {
for (const auto &str : td::rand_split(td::string(length / 16, '\0'))) { for (const auto &str : td::rand_split(td::string(length / 16, '\0'))) {
auto len = 16 * str.size(); auto len = 16 * str.size();
state.decrypt(td::Slice(t).substr(pos, len), td::MutableSlice(t).substr(pos, len)); state.decrypt(td::Slice(t).substr(pos, len), td::MutableSlice(t).substr(pos, len));
td::aes_cbc_decrypt(as_slice(key), as_slice(iv_copy), td::Slice(u).substr(pos, len), td::aes_cbc_decrypt(as_slice(key), as_mutable_slice(iv_copy), td::Slice(u).substr(pos, len),
td::MutableSlice(u).substr(pos, len)); td::MutableSlice(u).substr(pos, len));
pos += len; pos += len;
} }
@ -221,7 +221,7 @@ TEST(Crypto, Sha256State) {
for (auto length : {0, 1, 31, 32, 33, 9999, 10000, 10001, 999999, 1000001}) { for (auto length : {0, 1, 31, 32, 33, 9999, 10000, 10001, 999999, 1000001}) {
auto s = td::rand_string(std::numeric_limits<char>::min(), std::numeric_limits<char>::max(), length); auto s = td::rand_string(std::numeric_limits<char>::min(), std::numeric_limits<char>::max(), length);
td::UInt256 baseline; td::UInt256 baseline;
td::sha256(s, as_slice(baseline)); td::sha256(s, as_mutable_slice(baseline));
td::Sha256State state; td::Sha256State state;
state.init(); state.init();
@ -232,7 +232,7 @@ TEST(Crypto, Sha256State) {
} }
state = std::move(state2); state = std::move(state2);
td::UInt256 result; td::UInt256 result;
state.extract(as_slice(result)); state.extract(as_mutable_slice(result));
ASSERT_TRUE(baseline == result); ASSERT_TRUE(baseline == result);
} }
} }

View File

@ -832,7 +832,7 @@ class Master final : public Actor {
config.version_ = 12; config.version_ = 12;
auto storer = TLObjectStorer<my_api::messages_dhConfig>(config); auto storer = TLObjectStorer<my_api::messages_dhConfig>(config);
BufferSlice answer(storer.size()); BufferSlice answer(storer.size());
auto real_size = storer.store(answer.as_slice().ubegin()); auto real_size = storer.store(answer.as_mutable_slice().ubegin());
CHECK(real_size == answer.size()); CHECK(real_size == answer.size());
net_query->set_ok(std::move(answer)); net_query->set_ok(std::move(answer));
send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query)); send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query));
@ -857,7 +857,7 @@ class Master final : public Actor {
my_api::encryptedChat encrypted_chat(123, 321, 0, 1, 2, BufferSlice(), request_encryption.key_fingerprint_); my_api::encryptedChat encrypted_chat(123, 321, 0, 1, 2, BufferSlice(), request_encryption.key_fingerprint_);
auto storer = TLObjectStorer<my_api::encryptedChat>(encrypted_chat); auto storer = TLObjectStorer<my_api::encryptedChat>(encrypted_chat);
BufferSlice answer(storer.size()); BufferSlice answer(storer.size());
auto real_size = storer.store(answer.as_slice().ubegin()); auto real_size = storer.store(answer.as_mutable_slice().ubegin());
CHECK(real_size == answer.size()); CHECK(real_size == answer.size());
net_query->set_ok(std::move(answer)); net_query->set_ok(std::move(answer));
send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query)); send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query));
@ -899,8 +899,8 @@ class Master final : public Actor {
void process_net_query_send_encrypted(BufferSlice data, NetQueryPtr net_query, void process_net_query_send_encrypted(BufferSlice data, NetQueryPtr net_query,
ActorShared<NetQueryCallback> callback) { ActorShared<NetQueryCallback> callback) {
BufferSlice answer(8); BufferSlice answer(8);
answer.as_slice().fill(0); answer.as_mutable_slice().fill(0);
as<int32>(answer.as_slice().begin()) = static_cast<int32>(my_api::messages_sentEncryptedMessage::ID); as<int32>(answer.as_mutable_slice().begin()) = static_cast<int32>(my_api::messages_sentEncryptedMessage::ID);
net_query->set_ok(std::move(answer)); net_query->set_ok(std::move(answer));
send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query)); send_closure(std::move(callback), &NetQueryCallback::on_result, std::move(net_query));