diff options
author | Jack Lloyd <[email protected]> | 2020-11-05 07:38:24 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2020-11-05 07:38:24 -0500 |
commit | 5b20d7f8074083008e486894a19e802684ce63b4 (patch) | |
tree | 103c6025a4cdd5b57854481de075319c31d79a13 /src | |
parent | 3e20cb244e8687e1cf5462b643fc08814e3c01da (diff) | |
parent | f9f2cb003c2dd2c7f74dd120c15f39e1b3739430 (diff) |
Merge GH #2452 Simplify some functions in secmem.h
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/base/secmem.h | 57 | ||||
-rw-r--r-- | src/lib/utils/mem_ops.h | 29 |
2 files changed, 36 insertions, 50 deletions
diff --git a/src/lib/base/secmem.h b/src/lib/base/secmem.h index 68dd4c678..a5ba5857a 100644 --- a/src/lib/base/secmem.h +++ b/src/lib/base/secmem.h @@ -71,38 +71,7 @@ template<typename T> using SecureVector = secure_vector<T>; template<typename T> std::vector<T> unlock(const secure_vector<T>& in) { - std::vector<T> out(in.size()); - copy_mem(out.data(), in.data(), in.size()); - return out; - } - -template<typename T, typename Alloc> -size_t buffer_insert(std::vector<T, Alloc>& buf, - size_t buf_offset, - const T input[], - size_t input_length) - { - BOTAN_ASSERT_NOMSG(buf_offset <= buf.size()); - const size_t to_copy = std::min(input_length, buf.size() - buf_offset); - if(to_copy > 0) - { - copy_mem(&buf[buf_offset], input, to_copy); - } - return to_copy; - } - -template<typename T, typename Alloc, typename Alloc2> -size_t buffer_insert(std::vector<T, Alloc>& buf, - size_t buf_offset, - const std::vector<T, Alloc2>& input) - { - BOTAN_ASSERT_NOMSG(buf_offset <= buf.size()); - const size_t to_copy = std::min(input.size(), buf.size() - buf_offset); - if(to_copy > 0) - { - copy_mem(&buf[buf_offset], input.data(), to_copy); - } - return to_copy; + return std::vector<T>(in.begin(), in.end()); } template<typename T, typename Alloc, typename Alloc2> @@ -110,12 +79,8 @@ std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::vector<T, Alloc2>& in) { - const size_t copy_offset = out.size(); - out.resize(out.size() + in.size()); - if(in.size() > 0) - { - copy_mem(&out[copy_offset], in.data(), in.size()); - } + out.reserve(out.size() + in.size()); + out.insert(out.end(), in.begin(), in.end()); return out; } @@ -130,12 +95,8 @@ template<typename T, typename Alloc, typename L> std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<const T*, L>& in) { - const size_t copy_offset = out.size(); - out.resize(out.size() + in.second); - if(in.second > 0) - { - copy_mem(&out[copy_offset], in.first, in.second); - } + out.reserve(out.size() + in.second); + out.insert(out.end(), in.first, in.first + in.second); return out; } @@ -143,12 +104,8 @@ template<typename T, typename Alloc, typename L> std::vector<T, Alloc>& operator+=(std::vector<T, Alloc>& out, const std::pair<T*, L>& in) { - const size_t copy_offset = out.size(); - out.resize(out.size() + in.second); - if(in.second > 0) - { - copy_mem(&out[copy_offset], in.first, in.second); - } + out.reserve(out.size() + in.second); + out.insert(out.end(), in.first, in.first + in.second); return out; } diff --git a/src/lib/utils/mem_ops.h b/src/lib/utils/mem_ops.h index 15db4c365..deb4c01f2 100644 --- a/src/lib/utils/mem_ops.h +++ b/src/lib/utils/mem_ops.h @@ -224,6 +224,35 @@ template<typename T> inline bool same_mem(const T* p1, const T* p2, size_t n) return difference == 0; } +template<typename T, typename Alloc> +size_t buffer_insert(std::vector<T, Alloc>& buf, + size_t buf_offset, + const T input[], + size_t input_length) + { + BOTAN_ASSERT_NOMSG(buf_offset <= buf.size()); + const size_t to_copy = std::min(input_length, buf.size() - buf_offset); + if(to_copy > 0) + { + copy_mem(&buf[buf_offset], input, to_copy); + } + return to_copy; + } + +template<typename T, typename Alloc, typename Alloc2> +size_t buffer_insert(std::vector<T, Alloc>& buf, + size_t buf_offset, + const std::vector<T, Alloc2>& input) + { + BOTAN_ASSERT_NOMSG(buf_offset <= buf.size()); + const size_t to_copy = std::min(input.size(), buf.size() - buf_offset); + if(to_copy > 0) + { + copy_mem(&buf[buf_offset], input.data(), to_copy); + } + return to_copy; + } + /** * XOR arrays. Postcondition out[i] = in[i] ^ out[i] forall i = 0...length * @param out the input/output buffer |