Improve Slice::truncate usage.

This commit is contained in:
levlam 2021-02-04 15:54:12 +03:00
parent b07a9efb2e
commit e031a2c6c1
9 changed files with 15 additions and 14 deletions

View File

@ -32,9 +32,9 @@ class ProxySecret {
}
Slice get_proxy_secret() const {
auto proxy_secret = Slice(secret_).truncate(17);
if (proxy_secret.size() == 17) {
proxy_secret.remove_prefix(1);
Slice proxy_secret(secret_);
if (proxy_secret.size() >= 17) {
return proxy_secret.substr(1, 16);
}
return proxy_secret;
}

View File

@ -63,7 +63,7 @@ void IntermediateTransport::write_prepare_inplace(BufferWriter *message, bool qu
size_t append_size = 0;
if (with_padding()) {
append_size = Random::secure_uint32() % 16;
MutableSlice append = message->prepare_append().truncate(append_size);
MutableSlice append = message->prepare_append().substr(0, append_size);
CHECK(append.size() == append_size);
Random::secure_bytes(append);
message->confirm_append(append.size());

View File

@ -226,7 +226,7 @@ Status Transport::read_crypto_impl(int X, MutableSlice message, const AuthKey &a
auto *header = reinterpret_cast<HeaderT *>(message.begin());
*header_ptr = header;
auto to_decrypt = MutableSlice(header->encrypt_begin(), message.uend());
to_decrypt = to_decrypt.truncate(to_decrypt.size() & ~15);
to_decrypt.truncate(to_decrypt.size() & ~15);
if (to_decrypt.size() % 16 != 0) {
return Status::Error(PSLICE() << "Invalid mtproto message: size of encrypted part is not multiple of 16 [size = "
<< to_decrypt.size() << "]");

View File

@ -2041,7 +2041,7 @@ void SecretChatActor::calc_key_hash() {
auto sha256_slice = MutableSlice(sha256_buf, 32);
sha256(pfs_state_.auth_key.key(), sha256_slice);
auth_state_.key_hash = sha1_slice.truncate(16).str() + sha256_slice.truncate(20).str();
auth_state_.key_hash = PSTRING() << sha1_slice.substr(0, 16) << sha256_slice.substr(0, 20);
}
void SecretChatActor::send_update_secret_chat() {

View File

@ -367,7 +367,7 @@ Result<size_t> FileDownloader::process_part(Part part, NetQueryPtr net_query) {
bytes.as_slice());
}
auto slice = bytes.as_slice().truncate(part.size);
auto slice = bytes.as_slice().substr(0, part.size);
TRY_STATUS(acquire_fd());
LOG(INFO) << "Got " << slice.size() << " bytes at offset " << part.offset << " for \"" << path_ << '"';
TRY_RESULT(written, fd_.pwrite(slice, part.offset));

View File

@ -28,8 +28,7 @@ Status BinlogEvent::init(BufferSlice &&raw_event, bool check_crc) {
data_ = MutableSlice(const_cast<char *>(slice_data.begin()), slice_data.size());
crc32_ = static_cast<uint32>(parser.fetch_int());
if (check_crc) {
CHECK(size_ >= TAIL_SIZE);
auto calculated_crc = crc32(raw_event.as_slice().truncate(size_ - TAIL_SIZE));
auto calculated_crc = crc32(raw_event.as_slice().substr(0, size_ - TAIL_SIZE));
if (calculated_crc != crc32_) {
return Status::Error(PSLICE() << "crc mismatch " << tag("actual", format::as_hex(calculated_crc))
<< tag("expected", format::as_hex(crc32_)) << public_to_string());
@ -44,7 +43,7 @@ Status BinlogEvent::validate() const {
if (raw_event_.size() < 4) {
return Status::Error("Too small event");
}
uint32 size = TlParser(raw_event_.as_slice().truncate(4)).fetch_int();
uint32 size = TlParser(raw_event_.as_slice().substr(0, 4)).fetch_int();
if (size_ != size) {
return Status::Error(PSLICE() << "Size of event changed: " << tag("was", size_) << tag("now", size));
}

View File

@ -101,7 +101,8 @@ Result<size_t> BufferedFdBase<FdT>::flush_read(size_t max_read) {
CHECK(read_);
size_t result = 0;
while (::td::can_read_local(*this) && max_read) {
MutableSlice slice = read_->prepare_append().truncate(max_read);
MutableSlice slice = read_->prepare_append();
slice.truncate(max_read);
TRY_RESULT(x, FdT::read(slice));
slice.truncate(x);
read_->confirm_append(x);

View File

@ -36,7 +36,7 @@ class UdpWriter {
}
size_t cnt;
auto status = fd.send_messages(::td::Span<UdpSocketFd::OutboundMessage>(messages).truncate(to_send_n), cnt);
auto status = fd.send_messages(Span<UdpSocketFd::OutboundMessage>(messages).truncate(to_send_n), cnt);
queue.pop_n(cnt);
return status;
}
@ -51,7 +51,7 @@ class UdpReaderHelper {
buffer_ = BufferSlice(RESERVED_SIZE);
}
CHECK(buffer_.size() >= MAX_PACKET_SIZE);
message.data = buffer_.as_slice().truncate(MAX_PACKET_SIZE);
message.data = buffer_.as_slice().substr(0, MAX_PACKET_SIZE);
}
UdpMessage extract_udp_message(UdpSocketFd::InboundMessage &message) {

View File

@ -201,7 +201,8 @@ class BufferedStdinImpl {
size_t result = 0;
::td::sync_with_poll(*this);
while (::td::can_read_local(*this) && max_read) {
MutableSlice slice = writer_.prepare_append().truncate(max_read);
MutableSlice slice = writer_.prepare_append();
slice.truncate(max_read);
TRY_RESULT(x, file_fd_.read(slice));
slice.truncate(x);
writer_.confirm_append(x);