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

View File

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

View File

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

View File

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

View File

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