diff --git a/include/ircd/buffer.h b/include/ircd/buffer.h index 0e4753a03..5c9977a35 100644 --- a/include/ircd/buffer.h +++ b/include/ircd/buffer.h @@ -48,9 +48,14 @@ namespace ircd::buffer template struct buffer; struct const_buffer; struct mutable_buffer; + struct const_raw_buffer; + struct mutable_raw_buffer; template struct unique_buffer; + template class I> using const_buffers = I; template class I> using mutable_buffers = I; + template class I> using const_raw_buffers = I; + template class I> using mutable_raw_buffers = I; // Single buffer iteration of contents template const it &begin(const buffer &buffer); @@ -87,6 +92,8 @@ namespace ircd { using buffer::const_buffer; using buffer::mutable_buffer; + using buffer::const_raw_buffer; + using buffer::mutable_raw_buffer; using buffer::unique_buffer; using buffer::null_buffer; @@ -146,8 +153,6 @@ struct ircd::buffer::const_buffer const_buffer(const string_view &s) :buffer{std::begin(s), std::end(s)} {} - - const_buffer() = default; }; struct ircd::buffer::mutable_buffer @@ -158,6 +163,22 @@ struct ircd::buffer::mutable_buffer using buffer::buffer; }; +struct ircd::buffer::const_raw_buffer +:buffer +{ + operator boost::asio::const_buffer() const; + + using buffer::buffer; +}; + +struct ircd::buffer::mutable_raw_buffer +:buffer +{ + operator boost::asio::mutable_buffer() const; + + using buffer::buffer; +}; + template class buffers, class T> diff --git a/include/ircd/lexical.h b/include/ircd/lexical.h index ae4ad7020..bc577b6ce 100644 --- a/include/ircd/lexical.h +++ b/include/ircd/lexical.h @@ -91,17 +91,9 @@ namespace ircd // Binary / Hex / Base64 conversion suite // - // Binary buffer to null terminated string of hex (out must be 2*in + 1). - string_view u2a(const mutable_buffer &out, const uint8_t *const &in, const size_t &len); - string_view u2a(const mutable_buffer &out, const const_buffer &in); - - // String of hex to binary buffer. - const_buffer a2u(uint8_t *const &out, const size_t &max, const const_buffer &in); - const_buffer a2u(const mutable_buffer &out, const const_buffer &in); - - // Binary buffer to string of base64 - string_view b64encode(const mutable_buffer &out, const uint8_t *const &in, const size_t &len); - string_view b64encode(const mutable_buffer &out, const const_buffer &in); + string_view u2a(const mutable_buffer &out, const const_raw_buffer &in); + const_raw_buffer a2u(const mutable_raw_buffer &out, const const_buffer &in); + string_view b64encode(const mutable_buffer &out, const const_raw_buffer &in); // // String tokenization. diff --git a/ircd/lexical.cc b/ircd/lexical.cc index c37945b5e..29a264be4 100644 --- a/ircd/lexical.cc +++ b/ircd/lexical.cc @@ -237,20 +237,7 @@ ircd::tokens(const string_view &str, ircd::string_view ircd::b64encode(const mutable_buffer &out, - const const_buffer &in) -{ - const auto ptr - { - reinterpret_cast(data(in)) - }; - - return b64encode(out, ptr, size(in)); -} - -ircd::string_view -ircd::b64encode(const mutable_buffer &out, - const uint8_t *const &in, - const size_t &len) + const const_raw_buffer &in) { using transform = boost::archive::iterators::transform_width; @@ -258,26 +245,13 @@ ircd::b64encode(const mutable_buffer &out, using ostream_iterator = boost::archive::iterators::ostream_iterator; std::stringstream ss; - std::copy(b64fb(in), b64fb(in + len), ostream_iterator(ss)); + std::copy(b64fb(data(in)), b64fb(data(in) + size(in)), ostream_iterator(ss)); const auto outlen(ss.str().copy(data(out), size(out))); return { data(out), outlen }; } -ircd::const_buffer -ircd::a2u(const mutable_buffer &out, - const const_buffer &in) -{ - const auto ptr - { - reinterpret_cast(data(out)) - }; - - return a2u(ptr, size(out), in); -} - -ircd::const_buffer -ircd::a2u(uint8_t *const &out, - const size_t &max, +ircd::const_raw_buffer +ircd::a2u(const mutable_raw_buffer &out, const const_buffer &in) { const size_t len{size(in) / 2}; @@ -293,28 +267,15 @@ ircd::a2u(uint8_t *const &out, out[i] = strtol(gl, nullptr, 16); } - return { reinterpret_cast(out), len }; + return { data(out), len }; } ircd::string_view ircd::u2a(const mutable_buffer &out, - const const_buffer &in) -{ - const auto ptr - { - reinterpret_cast(data(in)) - }; - - return u2a(out, ptr, size(in)); -} - -ircd::string_view -ircd::u2a(const mutable_buffer &out, - const uint8_t *const &in, - const size_t &len) + const const_raw_buffer &in) { char *p(data(out)); - for(size_t i(0); i < len; ++i) + for(size_t i(0); i < size(in); ++i) p += snprintf(p, size(out) - (p - data(out)), "%02x", in[i]); return { data(out), size_t(p - data(out)) }; diff --git a/ircd/socket.cc b/ircd/socket.cc index a95599c2a..aa3bb4f3b 100644 --- a/ircd/socket.cc +++ b/ircd/socket.cc @@ -520,7 +520,8 @@ ircd::buffer::null_buffers null_buffer }}; -ircd::buffer::mutable_buffer::operator boost::asio::mutable_buffer() +ircd::buffer::mutable_buffer::operator +boost::asio::mutable_buffer() const { return boost::asio::mutable_buffer @@ -529,7 +530,28 @@ const }; } -ircd::buffer::const_buffer::operator boost::asio::const_buffer() +ircd::buffer::const_buffer::operator +boost::asio::const_buffer() +const +{ + return boost::asio::const_buffer + { + data(*this), size(*this) + }; +} + +ircd::buffer::mutable_raw_buffer::operator +boost::asio::mutable_buffer() +const +{ + return boost::asio::mutable_buffer + { + data(*this), size(*this) + }; +} + +ircd::buffer::const_raw_buffer::operator +boost::asio::const_buffer() const { return boost::asio::const_buffer