Use copy_from instead of memcoy if possible.

GitOrigin-RevId: 63981da8864d16ce46bca4b9580e7d9613c837c5
This commit is contained in:
levlam 2019-07-27 01:27:22 +03:00
parent cf329420db
commit f432abcce4
5 changed files with 16 additions and 29 deletions

View File

@ -17,7 +17,6 @@
// TODO: do I need \r\n as delimiter?
#include <cstring>
#include <tuple>
namespace td {
@ -73,8 +72,7 @@ void Transport::write(BufferWriter &&message, bool quick_ack) {
Slice src = r_head.ok();
// LOG(DEBUG) << src;
MutableSlice dst = message.prepare_prepend();
LOG_CHECK(dst.size() >= src.size()) << dst.size() << " >= " << src.size();
std::memcpy(dst.end() - src.size(), src.begin(), src.size());
dst.substr(dst.size() - src.size()).copy_from(src);
message.confirm_prepend(src.size());
output_->append(message.as_buffer_slice());
turn_ = Read;

View File

@ -23,8 +23,6 @@
#include <openssl/pem.h>
#include <openssl/rsa.h>
#include <cstring>
namespace td {
/*** RSA ***/
@ -113,8 +111,7 @@ size_t RSA::encrypt(unsigned char *from, size_t from_len, unsigned char *to) con
while (chunks-- > 0) {
BigNum x = BigNum::from_binary(Slice(from, 255));
BigNum::mod_exp(y, x, e_, n_, ctx);
string result = y.to_binary(256);
std::memcpy(to, result.c_str(), 256);
MutableSlice(to, 256).copy_from(y.to_binary(256));
to += 256;
}
return chunks * 256;
@ -126,8 +123,7 @@ void RSA::decrypt(Slice from, MutableSlice to) const {
BigNum x = BigNum::from_binary(from);
BigNum y;
BigNum::mod_exp(y, x, e_, n_, ctx);
string result = y.to_binary(256);
std::memcpy(to.data(), result.c_str(), 256);
to.copy_from(y.to_binary(256));
}
} // namespace td

View File

@ -11,8 +11,6 @@
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include <cstring>
namespace td {
class BufferedReader {
@ -34,13 +32,13 @@ inline Result<size_t> BufferedReader::read(MutableSlice slice) {
size_t available = end_pos_ - begin_pos_;
if (available >= slice.size()) {
// have enough data in buffer
std::memcpy(slice.begin(), &buff_[begin_pos_], slice.size());
slice.copy_from({&buff_[begin_pos_], slice.size()});
begin_pos_ += slice.size();
return slice.size();
}
if (available) {
std::memcpy(slice.begin(), &buff_[begin_pos_], available);
slice.copy_from({&buff_[begin_pos_], available});
begin_pos_ += available;
slice.remove_prefix(available);
}
@ -55,8 +53,8 @@ inline Result<size_t> BufferedReader::read(MutableSlice slice) {
end_pos_ = result;
size_t left = min(end_pos_, slice.size());
std::memcpy(slice.begin(), &buff_[begin_pos_], left);
begin_pos_ += left;
slice.copy_from({&buff_[0], left});
begin_pos_ = left;
return left + available;
}

View File

@ -13,7 +13,6 @@
#include "td/utils/Slice.h"
#include <atomic>
#include <cstring>
#include <limits>
#include <memory>
@ -120,7 +119,7 @@ class BufferSlice {
}
explicit BufferSlice(Slice slice) : BufferSlice(slice.size()) {
std::memcpy(as_slice().begin(), slice.begin(), slice.size());
as_slice().copy_from(slice);
}
BufferSlice(const char *ptr, size_t size) : BufferSlice(Slice(ptr, size)) {
@ -510,7 +509,7 @@ class ChainBufferIterator {
// copy to dest if possible
auto to_dest_size = min(ready.size(), dest.size());
if (to_dest_size != 0) {
std::memcpy(dest.data(), ready.data(), to_dest_size);
dest.copy_from(ready.substr(0, to_dest_size));
dest.remove_prefix(to_dest_size);
}
@ -691,7 +690,7 @@ class ChainBufferWriter {
while (!slice.empty()) {
auto ready = prepare_append(td::max(slice.size(), hint));
auto shift = min(ready.size(), slice.size());
std::memcpy(ready.data(), slice.data(), shift);
ready.copy_from(slice.substr(0, shift));
confirm_append(shift);
slice.remove_prefix(shift);
}

View File

@ -6,6 +6,7 @@
//
#include "td/utils/crypto.h"
#include "td/utils/as.h"
#include "td/utils/BigNum.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
@ -35,7 +36,6 @@
#endif
#include <algorithm>
#include <cstring>
#include <mutex>
#include <utility>
@ -143,20 +143,16 @@ void init_crypto() {
template <class FromT>
static string as_big_endian_string(const FromT &from) {
size_t size = sizeof(from);
string res(size, '\0');
char res[sizeof(FromT)];
as<FromT>(res) = from;
auto ptr = reinterpret_cast<const unsigned char *>(&from);
std::memcpy(&res[0], ptr, size);
size_t i = size;
size_t i = sizeof(FromT);
while (i && res[i - 1] == 0) {
i--;
}
res.resize(i);
std::reverse(res.begin(), res.end());
return res;
std::reverse(res, res + i);
return string(res, res + i);
}
static int pq_factorize_big(Slice pq_str, string *p_str, string *q_str) {