aboutsummaryrefslogtreecommitdiff
path: root/contrib/epee/include
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/epee/include')
-rw-r--r--contrib/epee/include/byte_slice.h36
-rw-r--r--contrib/epee/include/byte_stream.h224
-rw-r--r--contrib/epee/include/console_handler.h4
-rw-r--r--contrib/epee/include/md5_l.inl12
-rw-r--r--contrib/epee/include/net/abstract_tcp_server2.inl19
-rw-r--r--contrib/epee/include/net/http_base.h6
-rw-r--r--contrib/epee/include/net/http_client.h5
-rw-r--r--contrib/epee/include/net/http_server_impl_base.h2
-rw-r--r--contrib/epee/include/storages/levin_abstract_invoke2.h9
-rw-r--r--contrib/epee/include/storages/parserse_base_utils.h2
10 files changed, 293 insertions, 26 deletions
diff --git a/contrib/epee/include/byte_slice.h b/contrib/epee/include/byte_slice.h
index 1fbba101e..6b79f6d92 100644
--- a/contrib/epee/include/byte_slice.h
+++ b/contrib/epee/include/byte_slice.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2019, The Monero Project
+// Copyright (c) 2019-2020, The Monero Project
//
// All rights reserved.
//
@@ -39,10 +39,22 @@
namespace epee
{
struct byte_slice_data;
+ class byte_stream;
struct release_byte_slice
{
- void operator()(byte_slice_data*) const noexcept;
+ //! For use with `zmq_message_init_data`, use second arg for buffer pointer.
+ static void call(void*, void* ptr) noexcept;
+ void operator()(byte_slice_data* ptr) const noexcept
+ {
+ call(nullptr, ptr);
+ }
+ };
+
+ //! Frees ref count + buffer allocated internally by `byte_buffer`.
+ struct release_byte_buffer
+ {
+ void operator()(std::uint8_t* buf) const noexcept;
};
/*! Inspired by slices in golang. Storage is thread-safe reference counted,
@@ -99,6 +111,9 @@ namespace epee
//! Convert `buffer` into a slice using one allocation for shared count.
explicit byte_slice(std::string&& buffer);
+ //! Convert `stream` into a slice with zero allocations.
+ explicit byte_slice(byte_stream&& stream) noexcept;
+
byte_slice(byte_slice&& source) noexcept;
~byte_slice() noexcept = default;
@@ -140,6 +155,23 @@ namespace epee
\throw std::out_of_range If `size() < end`.
\return Slice starting at `data() + begin` of size `end - begin`. */
byte_slice get_slice(std::size_t begin, std::size_t end) const;
+
+ //! \post `empty()` \return Ownership of ref-counted buffer.
+ std::unique_ptr<byte_slice_data, release_byte_slice> take_buffer() noexcept;
};
+
+ //! Alias for a buffer that has space for a `byte_slice` ref count.
+ using byte_buffer = std::unique_ptr<std::uint8_t, release_byte_buffer>;
+
+ /*! \return `buf` with a new size of exactly `length`. New bytes not
+ initialized. A `nullptr` is returned on allocation failure. */
+ byte_buffer byte_buffer_resize(byte_buffer buf, std::size_t length) noexcept;
+
+ /*! Increase `buf` of size `current` by `more` bytes.
+
+ \throw std::range_error if `current + more` exceeds `size_t` bounds.
+ \return Buffer of `current + more` bytes. A `nullptr` is returned on
+ allocation failure. */
+ byte_buffer byte_buffer_increase(byte_buffer buf, std::size_t current, std::size_t more);
} // epee
diff --git a/contrib/epee/include/byte_stream.h b/contrib/epee/include/byte_stream.h
new file mode 100644
index 000000000..98f563ca9
--- /dev/null
+++ b/contrib/epee/include/byte_stream.h
@@ -0,0 +1,224 @@
+// Copyright (c) 2020, The Monero Project
+//
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other
+// materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its contributors may be
+// used to endorse or promote products derived from this software without specific
+// prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#pragma once
+
+#include <cassert>
+#include <cstdint>
+#include <cstring>
+
+#include "byte_slice.h"
+#include "span.h"
+
+namespace epee
+{
+ /*! \brief A partial drop-in replacement for `std::ostream`.
+
+ Only a few base `std::ostream` functions are implemented - enough for
+ rapidjson output currently.
+
+ Advantages over `std::stringstream` or `rapidjson::StringBuffer`:
+ - The internal buffer can be taken without a copy.
+ - The internal buffer can be given to `byte_slice` with zero
+ allocations for reference count.
+ - The internal buffer can be given to `zmq_msg_data_init` without a
+ copy or extra allocation.
+ an additional advantage over `std::stringstream`:
+ - Construction is significantly faster - the global `std::locale`
+ does not have to be acquired (global thread synchronization), and
+ an extra allocation for `std::stringbuf` is not needed (which is an
+ addition to the buffer inside of that object). */
+ class byte_stream
+ {
+ byte_buffer buffer_; //! Beginning of buffer
+ std::uint8_t* next_write_; //! Current write position
+ const std::uint8_t* end_; //! End of buffer
+ std::size_t increase_size_; //! Minimum buffer size increase
+
+ //! \post `requested <= available()`
+ void overflow(const std::size_t requested);
+
+ //! Ensures that at least `requested` bytes are available.
+ void check(const std::size_t requested)
+ {
+ const std::size_t remaining = available();
+ if (remaining < requested)
+ overflow(requested);
+ }
+
+ public:
+ using char_type = std::uint8_t;
+ using Ch = char_type;
+
+ //! \return Default minimum size increase on buffer overflow
+ static constexpr std::size_t default_increase() noexcept { return 4096; }
+
+ //! Increase internal buffer by at least `byte_stream_increase` bytes.
+ byte_stream() noexcept
+ : byte_stream(default_increase())
+ {}
+
+ //! Increase internal buffer by at least `increase` bytes.
+ explicit byte_stream(const std::size_t increase) noexcept
+ : buffer_(nullptr),
+ next_write_(nullptr),
+ end_(nullptr),
+ increase_size_(increase)
+ {}
+
+ byte_stream(byte_stream&& rhs) noexcept;
+ ~byte_stream() noexcept = default;
+ byte_stream& operator=(byte_stream&& rhs) noexcept;
+
+ //! \return The minimum increase size on buffer overflow
+ std::size_t increase_size() const noexcept { return increase_size_; }
+
+ const std::uint8_t* data() const noexcept { return buffer_.get(); }
+ std::uint8_t* tellp() const noexcept { return next_write_; }
+ std::size_t available() const noexcept { return end_ - next_write_; }
+ std::size_t size() const noexcept { return next_write_ - buffer_.get(); }
+ std::size_t capacity() const noexcept { return end_ - buffer_.get(); }
+
+ //! Compatibility with rapidjson.
+ void Flush() const noexcept
+ {}
+
+ /*! Reserve at least `more` bytes.
+ \post `size() + more <= available()`.
+ \throw std::range_error if exceeding max `size_t` value.
+ \throw std::bad_alloc if allocation fails. */
+ void reserve(const std::size_t more)
+ {
+ check(more);
+ }
+
+ /*! Copy `length` bytes starting at `ptr` to end of stream.
+ \throw std::range_error If exceeding max size_t value.
+ \throw std::bad_alloc If allocation fails. */
+ void write(const std::uint8_t* ptr, const std::size_t length)
+ {
+ check(length);
+ std::memcpy(tellp(), ptr, length);
+ next_write_ += length;
+ }
+
+ /*! Copy `length` bytes starting at `ptr` to end of stream.
+ \throw std::range_error if exceeding max `size_t` value.
+ \throw std::bad_alloc if allocation fails. */
+ void write(const char* ptr, const std::size_t length)
+ {
+ write(reinterpret_cast<const std::uint8_t*>(ptr), length);
+ }
+
+ /*! Copy `source` to end of stream.
+ \throw std::range_error if exceeding max `size_t` value.
+ \throw std::bad_alloc if allocation fails. */
+ void write(const epee::span<const std::uint8_t> source)
+ {
+ write(source.data(), source.size());
+ }
+
+ /*! Copy `source` to end of stream.
+ \throw std::range_error if exceeding max `size_t` value.
+ \throw std::bad_alloc if allocation fails. */
+ void write(const epee::span<const char> source)
+ {
+ write(source.data(), source.size());
+ }
+
+ /*! Copy `ch` to end of stream.
+ \throw std::range_error if exceeding max `size_t` value.
+ \throw std::bad_alloc if allocation fails. */
+ void put(const std::uint8_t ch)
+ {
+ check(1);
+ put_unsafe(ch);
+ }
+
+ /*! Copy `ch` to end of stream. Provides rapidjson compatability.
+ \throw std::range_error if exceeding max `size_t` value.
+ \throw std::bad_alloc if allocation fails. */
+ void Put(const std::uint8_t ch)
+ {
+ put(ch);
+ }
+
+ /*! Writes `ch` to end of stream without runtime capacity checks. Must use
+ `reserve` before calling this function. Primarily for use with
+ rapidjson, which writes characters at a time but reserves memory in
+ blocks. Most applications want to use `put` or `write`. */
+ void put_unsafe(const std::uint8_t ch) noexcept
+ {
+ assert(1 <= available());
+ *(tellp()) = ch;
+ ++next_write_;
+ }
+
+ /*! Write `ch` to end of stream `count` times.
+ \throw std::range_error if exceeding max `size_t` value.
+ \throw std::bad_alloc if allocation fails. */
+ void put_n(const std::uint8_t ch, const std::size_t count)
+ {
+ check(count);
+ std::memset(tellp(), count, ch);
+ next_write_ += count;
+ }
+
+ /*! Copy `ch` to end of stream.
+ \throw std::range_error if exceeding max `size_t` value.
+ \throw std::bad_alloc if allocation fails. */
+ void push_back(const std::uint8_t ch)
+ {
+ put(ch);
+ }
+
+ //! \return The internal buffer. \post `size() == capacity() == 0`.
+ byte_buffer take_buffer() noexcept;
+ };
+
+ //! Compatability/optimization for rapidjson.
+
+ inline void PutReserve(byte_stream& dest, const std::size_t length)
+ {
+ dest.reserve(length);
+ }
+
+ //! Compatability/optimization for rapidjson.
+
+ inline void PutUnsafe(byte_stream& dest, const std::uint8_t ch)
+ {
+ dest.put_unsafe(ch);
+ }
+
+ //! Compability/optimization for rapidjson.
+ inline void PutN(byte_stream& dest, const std::uint8_t ch, const std::size_t count)
+ {
+ dest.put_n(ch, count);
+ }
+} // epee
+
diff --git a/contrib/epee/include/console_handler.h b/contrib/epee/include/console_handler.h
index a7788aeb8..08d9b8802 100644
--- a/contrib/epee/include/console_handler.h
+++ b/contrib/epee/include/console_handler.h
@@ -465,7 +465,7 @@ eof:
bool run_default_console_handler_no_srv_param(t_server* ptsrv, t_handler handlr, std::function<std::string(void)> prompt, const std::string& usage = "")
{
async_console_handler console_handler;
- return console_handler.run(ptsrv, boost::bind<bool>(no_srv_param_adapter<t_server, t_handler>, _1, _2, handlr), prompt, usage);
+ return console_handler.run(ptsrv, std::bind<bool>(no_srv_param_adapter<t_server, t_handler>, std::placeholders::_1, std::placeholders::_2, handlr), prompt, usage);
}
template<class t_server, class t_handler>
@@ -634,7 +634,7 @@ eof:
bool run_handling(std::function<std::string(void)> prompt, const std::string& usage_string, std::function<void(void)> exit_handler = NULL)
{
- return m_console_handler.run(boost::bind(&console_handlers_binder::process_command_str, this, _1), prompt, usage_string, exit_handler);
+ return m_console_handler.run(std::bind(&console_handlers_binder::process_command_str, this, std::placeholders::_1), prompt, usage_string, exit_handler);
}
void print_prompt()
diff --git a/contrib/epee/include/md5_l.inl b/contrib/epee/include/md5_l.inl
index 8e339e006..cb2bd54f9 100644
--- a/contrib/epee/include/md5_l.inl
+++ b/contrib/epee/include/md5_l.inl
@@ -277,7 +277,7 @@ namespace md5
/* Zeroize sensitive information.
*/
- MD5_memset ((POINTER)context, 0, sizeof (*context));
+ memwipe ((POINTER)context, sizeof (*context));
}
/* MD5 basic transformation. Transforms state based on block.
@@ -369,7 +369,7 @@ namespace md5
/* Zeroize sensitive information.
*/
- MD5_memset ((POINTER)x, 0, sizeof (x));
+ memwipe ((POINTER)x, sizeof (x));
}
/* Note: Replace "for loop" with standard memcpy if possible.
@@ -431,9 +431,9 @@ namespace md5
MD5Update(&hmac->octx, k_opad, 64); /* apply outer pad */
/* scrub the pads and key context (if used) */
- MD5_memset( (POINTER)&k_ipad, 0, sizeof(k_ipad));
- MD5_memset( (POINTER)&k_opad, 0, sizeof(k_opad));
- MD5_memset( (POINTER)&tk, 0, sizeof(tk));
+ memwipe( (POINTER)&k_ipad, sizeof(k_ipad));
+ memwipe( (POINTER)&k_opad, sizeof(k_opad));
+ memwipe( (POINTER)&tk, sizeof(tk));
/* and we're done. */
}
@@ -459,7 +459,7 @@ namespace md5
state->istate[lupe] = htonl(hmac.ictx.state[lupe]);
state->ostate[lupe] = htonl(hmac.octx.state[lupe]);
}
- MD5_memset( (POINTER)&hmac, 0, sizeof(hmac));
+ memwipe( (POINTER)&hmac, sizeof(hmac));
}
diff --git a/contrib/epee/include/net/abstract_tcp_server2.inl b/contrib/epee/include/net/abstract_tcp_server2.inl
index 43ede3cc1..cbacd118c 100644
--- a/contrib/epee/include/net/abstract_tcp_server2.inl
+++ b/contrib/epee/include/net/abstract_tcp_server2.inl
@@ -32,7 +32,6 @@
-#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/chrono.hpp>
@@ -210,15 +209,15 @@ PRAGMA_WARNING_DISABLE_VS(4355)
socket().async_receive(boost::asio::buffer(buffer_),
boost::asio::socket_base::message_peek,
strand_.wrap(
- boost::bind(&connection<t_protocol_handler>::handle_receive, self,
- boost::asio::placeholders::error,
- boost::asio::placeholders::bytes_transferred)));
+ std::bind(&connection<t_protocol_handler>::handle_receive, self,
+ std::placeholders::_1,
+ std::placeholders::_2)));
else
async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(
- boost::bind(&connection<t_protocol_handler>::handle_read, self,
- boost::asio::placeholders::error,
- boost::asio::placeholders::bytes_transferred)));
+ std::bind(&connection<t_protocol_handler>::handle_read, self,
+ std::placeholders::_1,
+ std::placeholders::_2)));
#if !defined(_WIN32) || !defined(__i686)
// not supported before Windows7, too lazy for runtime check
// Just exclude for 32bit windows builds
@@ -688,7 +687,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
reset_timer(get_default_timeout(), false);
async_write(boost::asio::buffer(m_send_que.front().data(), size_now ) ,
strand_.wrap(
- boost::bind(&connection<t_protocol_handler>::handle_write, self, _1, _2)
+ std::bind(&connection<t_protocol_handler>::handle_write, self, std::placeholders::_1, std::placeholders::_2)
)
);
//_dbg3("(chunk): " << size_now);
@@ -892,7 +891,7 @@ PRAGMA_WARNING_DISABLE_VS(4355)
CHECK_AND_ASSERT_MES( size_now == m_send_que.front().size(), void(), "Unexpected queue size");
async_write(boost::asio::buffer(m_send_que.front().data(), size_now) ,
strand_.wrap(
- boost::bind(&connection<t_protocol_handler>::handle_write, connection<t_protocol_handler>::shared_from_this(), _1, _2)
+ std::bind(&connection<t_protocol_handler>::handle_write, connection<t_protocol_handler>::shared_from_this(), std::placeholders::_1, std::placeholders::_2)
)
);
//_dbg3("(normal)" << size_now);
@@ -1402,7 +1401,7 @@ POP_WARNINGS
shared_context->connect_mut.lock(); shared_context->ec = ec_; shared_context->cond.notify_one(); shared_context->connect_mut.unlock();
};
- sock_.async_connect(remote_endpoint, boost::bind<void>(connect_callback, _1, local_shared_context));
+ sock_.async_connect(remote_endpoint, std::bind<void>(connect_callback, std::placeholders::_1, local_shared_context));
while(local_shared_context->ec == boost::asio::error::would_block)
{
bool r = local_shared_context->cond.timed_wait(lock, boost::get_system_time() + boost::posix_time::milliseconds(conn_timeout));
diff --git a/contrib/epee/include/net/http_base.h b/contrib/epee/include/net/http_base.h
index a66fb7c23..bf6589c92 100644
--- a/contrib/epee/include/net/http_base.h
+++ b/contrib/epee/include/net/http_base.h
@@ -33,6 +33,7 @@
#include <string>
#include <utility>
+#include "memwipe.h"
#include "string_tools.h"
#undef MONERO_DEFAULT_LOG_CATEGORY
@@ -200,6 +201,11 @@ namespace net_utils
this->~http_response_info();
new(this) http_response_info();
}
+
+ void wipe()
+ {
+ memwipe(&m_body[0], m_body.size());
+ }
};
}
}
diff --git a/contrib/epee/include/net/http_client.h b/contrib/epee/include/net/http_client.h
index d329b8cf2..86df48f65 100644
--- a/contrib/epee/include/net/http_client.h
+++ b/contrib/epee/include/net/http_client.h
@@ -330,6 +330,11 @@ namespace net_utils
return m_net_client.get_bytes_received();
}
//---------------------------------------------------------------------------
+ void wipe_response()
+ {
+ m_response_info.wipe();
+ }
+ //---------------------------------------------------------------------------
private:
//---------------------------------------------------------------------------
inline bool handle_reciev(std::chrono::milliseconds timeout)
diff --git a/contrib/epee/include/net/http_server_impl_base.h b/contrib/epee/include/net/http_server_impl_base.h
index 6cd19f17b..d88b53c94 100644
--- a/contrib/epee/include/net/http_server_impl_base.h
+++ b/contrib/epee/include/net/http_server_impl_base.h
@@ -31,7 +31,7 @@
#include <boost/thread.hpp>
-#include <boost/bind.hpp>
+#include <boost/bind/bind.hpp>
#include "net/abstract_tcp_server2.h"
#include "http_protocol_handler.h"
diff --git a/contrib/epee/include/storages/levin_abstract_invoke2.h b/contrib/epee/include/storages/levin_abstract_invoke2.h
index b18e04a27..4633fa546 100644
--- a/contrib/epee/include/storages/levin_abstract_invoke2.h
+++ b/contrib/epee/include/storages/levin_abstract_invoke2.h
@@ -28,6 +28,7 @@
#include "portable_storage_template_helper.h"
#include <boost/utility/value_init.hpp>
+#include <functional>
#include "span.h"
#include "net/levin_base.h"
@@ -294,20 +295,20 @@ namespace epee
#define HANDLE_INVOKE2(command_id, func, type_name_in, typename_out) \
if(!is_notify && command_id == command) \
- {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, type_name_in, typename_out>(this, command, in_buff, buff_out, boost::bind(func, this, _1, _2, _3, _4), context);}
+ {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, type_name_in, typename_out>(this, command, in_buff, buff_out, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), context);}
#define HANDLE_INVOKE_T2(COMMAND, func) \
if(!is_notify && COMMAND::ID == command) \
- {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, typename COMMAND::request, typename COMMAND::response>(command, in_buff, buff_out, boost::bind(func, this, _1, _2, _3, _4), context);}
+ {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, typename COMMAND::request, typename COMMAND::response>(command, in_buff, buff_out, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), context);}
#define HANDLE_NOTIFY2(command_id, func, type_name_in) \
if(is_notify && command_id == command) \
- {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, type_name_in>(this, command, in_buff, boost::bind(func, this, _1, _2, _3), context);}
+ {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, type_name_in>(this, command, in_buff, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), context);}
#define HANDLE_NOTIFY_T2(NOTIFY, func) \
if(is_notify && NOTIFY::ID == command) \
- {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, typename NOTIFY::request>(this, command, in_buff, boost::bind(func, this, _1, _2, _3), context);}
+ {handled=true;return epee::net_utils::buff_to_t_adapter<internal_owner_type_name, typename NOTIFY::request>(this, command, in_buff, std::bind(func, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3), context);}
#define CHAIN_INVOKE_MAP2(func) \
diff --git a/contrib/epee/include/storages/parserse_base_utils.h b/contrib/epee/include/storages/parserse_base_utils.h
index 2256f6b83..5a6cc0b51 100644
--- a/contrib/epee/include/storages/parserse_base_utils.h
+++ b/contrib/epee/include/storages/parserse_base_utils.h
@@ -196,7 +196,7 @@ namespace misc_utils
uint32_t dst = 0;
for (int i = 0; i < 4; ++i)
{
- const unsigned char tmp = isx[(int)*++it];
+ const unsigned char tmp = isx[(unsigned char)*++it];
CHECK_AND_ASSERT_THROW_MES(tmp != 0xff, "Bad Unicode encoding");
dst = dst << 4 | tmp;
}