0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 10:08:56 +02:00

ircd: Eliminate the raw_buffer concept.

This commit is contained in:
Jason Volk 2018-02-02 23:20:26 -08:00
parent 287331d757
commit d53eef4ab6
26 changed files with 247 additions and 343 deletions

View file

@ -15,26 +15,26 @@ namespace ircd
{
// Binary -> Base58 encode suite
constexpr size_t b58encode_size(const size_t &);
size_t b58encode_size(const const_raw_buffer &in);
string_view b58encode(const mutable_buffer &out, const const_raw_buffer &in);
std::string b58encode(const const_raw_buffer &in);
size_t b58encode_size(const const_buffer &in);
string_view b58encode(const mutable_buffer &out, const const_buffer &in);
std::string b58encode(const const_buffer &in);
// Base58 -> Binary decode suite
constexpr size_t b58decode_size(const size_t &);
size_t b58decode_size(const string_view &in);
const_raw_buffer b58decode(const mutable_raw_buffer &out, const string_view &in);
const_buffer b58decode(const mutable_buffer &out, const string_view &in);
std::string b58decode(const string_view &in);
// Binary -> Base64 conversion suite
string_view b64encode(const mutable_buffer &out, const const_raw_buffer &in);
std::string b64encode(const const_raw_buffer &in);
string_view b64encode(const mutable_buffer &out, const const_buffer &in);
std::string b64encode(const const_buffer &in);
// Binary -> Base64 conversion without padding
string_view b64encode_unpadded(const mutable_buffer &out, const const_raw_buffer &in);
std::string b64encode_unpadded(const const_raw_buffer &in);
string_view b64encode_unpadded(const mutable_buffer &out, const const_buffer &in);
std::string b64encode_unpadded(const const_buffer &in);
// Base64 -> Binary conversion (padded or unpadded)
const_raw_buffer b64decode(const mutable_raw_buffer &out, const string_view &in);
const_buffer b64decode(const mutable_buffer &out, const string_view &in);
std::string b64decode(const string_view &in);
}
@ -51,7 +51,7 @@ ircd::b58decode_size(const size_t &in)
}
inline size_t
ircd::b58encode_size(const const_raw_buffer &in)
ircd::b58encode_size(const const_buffer &in)
{
return b58encode_size(size(in));
}

View file

@ -34,13 +34,7 @@ namespace boost::asio
/// Lightweight buffer interface compatible with boost::asio IO buffers and vectors
///
/// A const_buffer is a pair of iterators like `const char *` meant for sending
/// data; a mutable_buffer is a pair of iterators meant for receiving. In fact,
/// those types store signed char* and our convention is to represent readable
/// data with them. The const_raw_buffer and mutable_raw_buffer use unsigned
/// char* pairs and our convention is to represent unreadable/binary data with
/// them. Remember, these are conventions, not guarantees from these types. The
/// const_buffer is analogous (but doesn't inherit from) a string_view, so they
/// play well and convert easily between each other.
/// data; a mutable_buffer is a pair of iterators meant for receiving.
///
/// These templates offer tools for individual buffers as well as tools for
/// iterations of buffers. An iteration of buffers is an iovector that is
@ -53,21 +47,14 @@ namespace ircd::buffer
template<class it> struct buffer;
struct const_buffer;
struct mutable_buffer;
struct const_raw_buffer;
struct mutable_raw_buffer;
struct stream_buffer;
template<class buffer, size_t SIZE> struct fixed_buffer;
template<class buffer, uint align = 16> struct unique_buffer;
struct stream_buffer;
template<size_t SIZE> using fixed_const_buffer = fixed_buffer<const_buffer, SIZE>;
template<size_t SIZE> using fixed_mutable_buffer = fixed_buffer<mutable_buffer, SIZE>;
template<size_t SIZE> using fixed_const_raw_buffer = fixed_buffer<const_raw_buffer, SIZE>;
template<size_t SIZE> using fixed_mutable_raw_buffer = fixed_buffer<mutable_raw_buffer, SIZE>;
template<template<class> class I> using const_buffers = I<const_buffer>;
template<template<class> class I> using mutable_buffers = I<mutable_buffer>;
template<template<class> class I> using const_raw_buffers = I<const_raw_buffer>;
template<template<class> class I> using mutable_raw_buffers = I<mutable_raw_buffer>;
// Single buffer iteration of contents
template<class it> const it &begin(const buffer<it> &buffer);
@ -85,15 +72,15 @@ namespace ircd::buffer
template<class it> size_t size(const buffer<it> &buffer);
template<class it> const it &data(const buffer<it> &buffer);
template<class it> size_t consume(buffer<it> &buffer, const size_t &bytes);
template<class it> it copy(it &dest, const it &stop, const const_raw_buffer &);
size_t copy(const mutable_raw_buffer &dst, const const_raw_buffer &src);
size_t reverse(const mutable_raw_buffer &dst, const const_raw_buffer &src);
void reverse(const mutable_raw_buffer &buf);
void zero(const mutable_raw_buffer &buf);
template<class it> it copy(it &dest, const it &stop, const const_buffer &);
size_t copy(const mutable_buffer &dst, const const_buffer &src);
size_t reverse(const mutable_buffer &dst, const const_buffer &src);
void reverse(const mutable_buffer &buf);
void zero(const mutable_buffer &buf);
// Iterable of buffers tools
template<template<class> class I, class T> size_t size(const I<T> &buffers);
template<template<class> class I, class T> size_t copy(const mutable_raw_buffer &, const I<T> &buffer);
template<template<class> class I, class T> size_t copy(const mutable_buffer &, const I<T> &buffer);
template<template<class> class I, class T> size_t consume(I<T> &buffers, const size_t &bytes);
// Convenience copy to std stream
@ -110,16 +97,12 @@ namespace ircd
{
using buffer::const_buffer;
using buffer::mutable_buffer;
using buffer::const_raw_buffer;
using buffer::mutable_raw_buffer;
using buffer::fixed_buffer;
using buffer::unique_buffer;
using buffer::null_buffer;
using buffer::stream_buffer;
using buffer::fixed_const_buffer;
using buffer::fixed_mutable_buffer;
using buffer::fixed_const_raw_buffer;
using buffer::fixed_mutable_raw_buffer;
using buffer::const_buffers;
using buffer::mutable_buffers;
@ -171,20 +154,17 @@ struct ircd::buffer::buffer
{}
};
namespace ircd::buffer
{
template<class T> struct mutable_buffer_base;
}
/// Base for mutable buffers, or buffers which can be written to because they
/// are not const.
///
template<class T>
struct ircd::buffer::mutable_buffer_base
:buffer<T>
struct ircd::buffer::mutable_buffer
:buffer<char *>
{
using iterator = typename buffer<T>::iterator;
using value_type = typename buffer<T>::value_type;
using iterator = typename buffer<char *>::iterator;
using value_type = typename buffer<char *>::value_type;
// Conversion offered for the analogous asio buffer
operator boost::asio::mutable_buffer() const;
// Allows boost::spirit to append to the buffer; this means the size() of
// this buffer becomes a consumption counter and the real size of the buffer
@ -197,151 +177,70 @@ struct ircd::buffer::mutable_buffer_base
++std::get<1>(*this);
}
using buffer<T>::buffer;
using buffer<char *>::buffer;
mutable_buffer_base()
mutable_buffer()
:buffer<iterator>{}
{}
template<size_t SIZE>
mutable_buffer_base(value_type (&buf)[SIZE])
mutable_buffer(value_type (&buf)[SIZE])
:buffer<iterator>{buf, SIZE}
{}
template<size_t SIZE>
mutable_buffer_base(std::array<value_type, SIZE> &buf)
mutable_buffer(std::array<value_type, SIZE> &buf)
:buffer<iterator>{reinterpret_cast<iterator>(buf.data()), SIZE}
{}
// lvalue string reference offered to write through to a std::string as
// the buffer. not explicit; should be hard to bind by accident...
mutable_buffer_base(std::string &buf)
:mutable_buffer_base<iterator>{const_cast<iterator>(buf.data()), buf.size()}
mutable_buffer(std::string &buf)
:mutable_buffer{const_cast<iterator>(buf.data()), buf.size()}
{}
};
/// A writable buffer of signed char data. Convention is for this buffer to
/// represent readable strings, which may or may not be null terminated. This
/// is just a convention; not a gurantee of the type.
///
struct ircd::buffer::mutable_buffer
:mutable_buffer_base<char *>
{
// Conversion offered for the analogous asio buffer
operator boost::asio::mutable_buffer() const;
using mutable_buffer_base<char *>::mutable_buffer_base;
mutable_buffer(const std::function<void (const mutable_buffer &)> &closure)
{
closure(*this);
}
mutable_buffer() = default;
};
/// A writable buffer of unsigned signed char data. Convention is for this
/// buffer to represent unreadable binary data. It may also be constructed
/// from a mutable_buffer because a buffer of any data (this one) can also
/// be readable data. The inverse is not true, mutable_buffer cannot be
/// constructed from this class.
///
struct ircd::buffer::mutable_raw_buffer
:mutable_buffer_base<unsigned char *>
struct ircd::buffer::const_buffer
:buffer<const char *>
{
// Conversion offered for the analogous asio buffer
operator boost::asio::mutable_buffer() const;
using mutable_buffer_base<unsigned char *>::mutable_buffer_base;
mutable_raw_buffer(const mutable_buffer &b)
:mutable_buffer_base<iterator>{reinterpret_cast<iterator>(data(b)), size(b)}
{}
mutable_raw_buffer(const std::function<void (const mutable_raw_buffer &)> &closure)
{
closure(*this);
}
mutable_raw_buffer() = default;
};
namespace ircd::buffer
{
template<class T> struct const_buffer_base;
}
template<class T>
struct ircd::buffer::const_buffer_base
:buffer<T>
{
using iterator = typename buffer<T>::iterator;
using value_type = typename buffer<T>::value_type;
using iterator = typename buffer<const char *>::iterator;
using value_type = typename buffer<const char *>::value_type;
using mutable_value_type = typename std::remove_const<value_type>::type;
using buffer<T>::buffer;
operator boost::asio::const_buffer() const;
const_buffer_base()
using buffer<const char *>::buffer;
const_buffer()
:buffer<iterator>{}
{}
template<size_t SIZE>
const_buffer_base(const value_type (&buf)[SIZE])
const_buffer(const value_type (&buf)[SIZE])
:buffer<iterator>{buf, SIZE}
{}
template<size_t SIZE>
const_buffer_base(const std::array<mutable_value_type, SIZE> &buf)
const_buffer(const std::array<mutable_value_type, SIZE> &buf)
:buffer<iterator>{reinterpret_cast<iterator>(buf.data()), SIZE}
{}
const_buffer_base(const mutable_buffer &b)
const_buffer(const mutable_buffer &b)
:buffer<iterator>{reinterpret_cast<iterator>(data(b)), size(b)}
{}
const_buffer_base(const string_view &s)
const_buffer(const string_view &s)
:buffer<iterator>{reinterpret_cast<iterator>(s.data()), s.size()}
{}
explicit const_buffer_base(const std::string &s)
:buffer<iterator>{reinterpret_cast<iterator>(s.data()), s.size()}
{}
};
struct ircd::buffer::const_buffer
:const_buffer_base<const char *>
{
operator boost::asio::const_buffer() const;
using const_buffer_base<iterator>::const_buffer_base;
explicit const_buffer(const std::string &s)
:const_buffer_base{s}
:buffer<iterator>{reinterpret_cast<iterator>(s.data()), s.size()}
{}
const_buffer() = default;
};
struct ircd::buffer::const_raw_buffer
:const_buffer_base<const unsigned char *>
{
operator boost::asio::const_buffer() const;
using const_buffer_base<iterator>::const_buffer_base;
const_raw_buffer(const const_buffer &b)
:const_buffer_base{reinterpret_cast<iterator>(data(b)), size(b)}
{}
const_raw_buffer(const mutable_raw_buffer &b)
:const_buffer_base{reinterpret_cast<iterator>(data(b)), size(b)}
{}
explicit const_raw_buffer(const std::string &s)
:const_buffer_base{reinterpret_cast<iterator>(s.data()), s.size()}
{}
const_raw_buffer() = default;
};
/// fixed_buffer wraps an std::array with construction and conversions apropos
@ -585,7 +484,7 @@ template<template<class>
class buffers,
class T>
size_t
ircd::buffer::copy(const mutable_raw_buffer &dest,
ircd::buffer::copy(const mutable_buffer &dest,
const buffers<T> &b)
{
using it = typename T::iterator;
@ -622,14 +521,14 @@ ircd::buffer::operator<<(std::ostream &s, const buffer<it> &buffer)
}
inline void
ircd::buffer::reverse(const mutable_raw_buffer &buf)
ircd::buffer::reverse(const mutable_buffer &buf)
{
std::reverse(data(buf), data(buf) + size(buf));
}
inline size_t
ircd::buffer::reverse(const mutable_raw_buffer &dst,
const const_raw_buffer &src)
ircd::buffer::reverse(const mutable_buffer &dst,
const const_buffer &src)
{
const size_t ret{std::min(size(dst), size(src))};
std::reverse_copy(data(src), data(src) + ret, data(dst));
@ -637,8 +536,8 @@ ircd::buffer::reverse(const mutable_raw_buffer &dst,
}
inline size_t
ircd::buffer::copy(const mutable_raw_buffer &dst,
const const_raw_buffer &src)
ircd::buffer::copy(const mutable_buffer &dst,
const const_buffer &src)
{
auto e{begin(dst)};
copy(e, end(dst), src);
@ -650,7 +549,7 @@ template<class it>
it
ircd::buffer::copy(it &dest,
const it &stop,
const const_raw_buffer &src)
const const_buffer &src)
{
const it ret{dest};
const ssize_t srcsz(size(src));

View file

@ -44,14 +44,12 @@ namespace ircd::db
bool has(column &, const string_view &key, const gopts & = {});
// [GET] Convenience functions to copy data into your buffer.
// The signed char buffer is null terminated; the unsigned is not.
size_t read(column &, const string_view &key, const mutable_raw_buffer &, const gopts & = {});
string_view read(column &, const string_view &key, const mutable_buffer &, const gopts & = {});
std::string read(column &, const string_view &key, const gopts & = {});
// [SET] Write data to the db
void write(column &, const string_view &key, const string_view &value, const sopts & = {});
void write(column &, const string_view &key, const mutable_raw_buffer &, const sopts & = {});
void write(column &, const string_view &key, const mutable_buffer &, const sopts & = {});
// [SET] Remove data from the db. not_found is never thrown.
void del(column &, const string_view &key, const sopts & = {});

View file

@ -45,31 +45,31 @@ class ircd::ed25519::sk
std::unique_ptr<uint8_t[], void (*)(void *)> key;
public:
sig sign(const const_raw_buffer &msg) const;
sig sign(const const_buffer &msg) const;
sk(const std::string &filename, pk *const & = nullptr);
sk(pk *const &, const const_raw_buffer &seed);
sk(pk *const &, const const_buffer &seed);
sk(): key{nullptr, std::free} {}
};
struct ircd::ed25519::pk
:fixed_mutable_raw_buffer<PK_SZ>
:fixed_mutable_buffer<PK_SZ>
{
bool verify(const const_raw_buffer &msg, const sig &) const;
bool verify(const const_buffer &msg, const sig &) const;
using fixed_mutable_raw_buffer<PK_SZ>::fixed_mutable_raw_buffer;
using fixed_mutable_buffer<PK_SZ>::fixed_mutable_buffer;
pk()
:fixed_mutable_raw_buffer<PK_SZ>{nullptr}
:fixed_mutable_buffer<PK_SZ>{nullptr}
{}
};
struct ircd::ed25519::sig
:fixed_mutable_raw_buffer<SIG_SZ>
:fixed_mutable_buffer<SIG_SZ>
{
using fixed_mutable_raw_buffer<SIG_SZ>::fixed_mutable_raw_buffer;
using fixed_mutable_buffer<SIG_SZ>::fixed_mutable_buffer;
sig()
:fixed_mutable_raw_buffer<SIG_SZ>{nullptr}
:fixed_mutable_buffer<SIG_SZ>{nullptr}
{}
};

View file

@ -94,10 +94,10 @@ namespace ircd::fs
bool mkdir(const std::string &path);
// This suite of IO functions may yield your context.
bool write(const std::string &name, const const_raw_buffer &buf);
bool append(const std::string &name, const const_raw_buffer &buf);
bool overwrite(const std::string &name, const const_raw_buffer &buf);
bool overwrite(const string_view &name, const const_raw_buffer &buf);
bool write(const std::string &name, const const_buffer &buf);
bool append(const std::string &name, const const_buffer &buf);
bool overwrite(const std::string &name, const const_buffer &buf);
bool overwrite(const string_view &name, const const_buffer &buf);
extern aio *aioctx;
}

View file

@ -25,7 +25,7 @@ namespace ircd::fs
struct read_opts extern const read_opts_default;
// Yields ircd::ctx for read into buffer; returns view of read portion.
string_view read(const string_view &path, const mutable_raw_buffer &, const read_opts & = read_opts_default);
string_view read(const string_view &path, const mutable_buffer &, const read_opts & = read_opts_default);
// Yields ircd::ctx for read into allocated string; returns that string
std::string read(const string_view &path, const read_opts & = read_opts_default);

View file

@ -64,28 +64,28 @@ namespace ircd
/// Use this type when dealing with algorithm-agnostic hashing.
struct ircd::crh::hash
{
/// Returns the byte length of the mutable_raw_buffer for digests
/// Returns the byte length of the mutable_buffer for digests
virtual size_t length() const = 0;
/// Samples the digest at the current state (without modifying)
virtual void digest(const mutable_raw_buffer &) const = 0;
virtual void digest(const mutable_buffer &) const = 0;
/// Samples the digest and modifies the state (depending on impl)
virtual void finalize(const mutable_raw_buffer &b)
virtual void finalize(const mutable_buffer &b)
{
digest(b);
}
/// Appends to the message
virtual void update(const const_raw_buffer &) = 0;
virtual void update(const const_buffer &) = 0;
// conveniences for output
template<size_t SIZE> fixed_const_raw_buffer<SIZE> digest() const;
template<size_t SIZE> operator fixed_const_raw_buffer<SIZE>() const;
template<size_t SIZE> fixed_const_buffer<SIZE> digest() const;
template<size_t SIZE> operator fixed_const_buffer<SIZE>() const;
// conveniences for input
void operator()(const mutable_raw_buffer &out, const const_raw_buffer &in);
hash &operator+=(const const_raw_buffer &);
void operator()(const mutable_buffer &out, const const_buffer &in);
hash &operator+=(const const_buffer &);
virtual ~hash() noexcept;
};
@ -102,19 +102,19 @@ final
256 / 8
};
using buf = fixed_const_raw_buffer<digest_size>;
using buf = fixed_const_buffer<digest_size>;
protected:
std::unique_ptr<ctx> ctx;
public:
size_t length() const override;
void digest(const mutable_raw_buffer &) const override;
void finalize(const mutable_raw_buffer &) override;
void update(const const_raw_buffer &) override;
void digest(const mutable_buffer &) const override;
void finalize(const mutable_buffer &) override;
void update(const const_buffer &) override;
sha256(const mutable_raw_buffer &, const const_raw_buffer &);
sha256(const const_raw_buffer &);
sha256(const mutable_buffer &, const const_buffer &);
sha256(const const_buffer &);
sha256();
~sha256() noexcept;
};
@ -122,7 +122,7 @@ final
/// Automatic gratification from hash::digest()
template<size_t SIZE>
ircd::crh::hash::operator
fixed_const_raw_buffer<SIZE>()
fixed_const_buffer<SIZE>()
const
{
return digest<SIZE>();
@ -130,12 +130,12 @@ const
/// Digests the hash into the buffer of the specified SIZE and returns it
template<size_t SIZE>
ircd::fixed_const_raw_buffer<SIZE>
ircd::fixed_const_buffer<SIZE>
ircd::crh::hash::digest()
const
{
assert(SIZE >= length());
return fixed_const_raw_buffer<SIZE>
return fixed_const_buffer<SIZE>
{
[this](const auto &buffer)
{

View file

@ -1097,7 +1097,7 @@ const
{
[&preimage](auto &buf)
{
sha256{buf, const_raw_buffer{preimage}};
sha256{buf, const_buffer{preimage}};
}
};
}
@ -1117,7 +1117,7 @@ sign(const tuple &t,
{
[&sk, &preimage](auto &buf)
{
sk.sign(buf, const_raw_buffer{preimage});
sk.sign(buf, const_buffer{preimage});
}
};
}
@ -1136,7 +1136,7 @@ noexcept try
json::strung(t)
};
return pk.verify(const_raw_buffer{preimage}, sig);
return pk.verify(const_buffer{preimage}, sig);
}
catch(const std::exception &e)
{

View file

@ -45,9 +45,9 @@ namespace ircd
template<class T> string_view lex_cast(const T &t);
// Binary <-> Hex conversion suite
const_raw_buffer a2u(const mutable_raw_buffer &out, const const_buffer &in);
string_view u2a(const mutable_buffer &out, const const_raw_buffer &in);
std::string u2a(const const_raw_buffer &in);
const_buffer a2u(const mutable_buffer &out, const const_buffer &in);
string_view u2a(const mutable_buffer &out, const const_buffer &in);
std::string u2a(const const_buffer &in);
}
namespace ircd

View file

@ -39,7 +39,7 @@ namespace ircd::m
std::string pretty(const event &);
std::string pretty_oneline(const event &);
id::event event_id(const event &, id::event::buf &buf, const const_raw_buffer &hash);
id::event event_id(const event &, id::event::buf &buf, const const_buffer &hash);
id::event event_id(const event &, id::event::buf &buf);
id::event event_id(const event &);
}

View file

@ -65,7 +65,7 @@ namespace ircd::net
size_t available(const socket &) noexcept;
ipport local_ipport(const socket &) noexcept;
ipport remote_ipport(const socket &) noexcept;
const_raw_buffer peer_cert_der(const mutable_raw_buffer &, const socket &);
const_buffer peer_cert_der(const mutable_buffer &, const socket &);
}
// Exports to ircd::

View file

@ -98,8 +98,8 @@ namespace ircd::openssl
void genec(const string_view &skfile, const string_view &pkfile, const EC_GROUP *const & = secp256k1);
// X.509 suite
const_raw_buffer i2d(const mutable_raw_buffer &out, const X509 &);
const_raw_buffer cert2d(const mutable_raw_buffer &out, const string_view &pem);
const_buffer i2d(const mutable_buffer &out, const X509 &);
const_buffer cert2d(const mutable_buffer &out, const string_view &pem);
X509 &read_pem(X509 &out, const string_view &pem);
string_view write_pem(const mutable_buffer &out, const X509 &);
string_view print(const mutable_buffer &buf, const X509 &, ulong flags = -1);
@ -142,7 +142,7 @@ namespace ircd::openssl::bio
namespace ircd::openssl
{
size_t size(const BIGNUM *const &); // bytes binary
mutable_raw_buffer data(const mutable_raw_buffer &out, const BIGNUM *const &); // le
mutable_buffer data(const mutable_buffer &out, const BIGNUM *const &); // le
string_view u2a(const mutable_buffer &out, const BIGNUM *const &);
}
@ -177,7 +177,7 @@ class ircd::openssl::bignum
{}
explicit bignum(const uint128_t &val);
bignum(const const_raw_buffer &bin); // le
bignum(const const_buffer &bin); // le
explicit bignum(const BIGNUM &a);
bignum(const bignum &);
bignum(bignum &&) noexcept;

View file

@ -286,7 +286,7 @@ catch(const std::exception &e)
namespace ircd::fs
{
string_view read__aio(const string_view &path, const mutable_raw_buffer &, const read_opts &);
string_view read__aio(const string_view &path, const mutable_buffer &, const read_opts &);
std::string read__aio(const string_view &path, const read_opts &);
}
@ -294,11 +294,11 @@ namespace ircd::fs
struct ircd::fs::aio::request::read
:request
{
read(const int &fd, const mutable_raw_buffer &buf, const read_opts &opts);
read(const int &fd, const mutable_buffer &buf, const read_opts &opts);
};
ircd::fs::aio::request::read::read(const int &fd,
const mutable_raw_buffer &buf,
const mutable_buffer &buf,
const read_opts &opts)
:request{fd}
{
@ -348,7 +348,7 @@ ircd::fs::read__aio(const string_view &path,
ircd::string_view
ircd::fs::read__aio(const string_view &path,
const mutable_raw_buffer &buf,
const mutable_buffer &buf,
const read_opts &opts)
{
// Path to open(2) must be null terminated;

View file

@ -19,13 +19,13 @@ namespace ircd
'='
};
using _b64_encoder = std::function<string_view (const mutable_buffer &, const const_raw_buffer &)>;
static std::string _b64encode(const const_raw_buffer &in, const _b64_encoder &);
using _b64_encoder = std::function<string_view (const mutable_buffer &, const const_buffer &)>;
static std::string _b64encode(const const_buffer &in, const _b64_encoder &);
}
/// Allocate and return a string without padding from the encoding of in
std::string
ircd::b64encode_unpadded(const const_raw_buffer &in)
ircd::b64encode_unpadded(const const_buffer &in)
{
return _b64encode(in, [](const auto &out, const auto &in)
{
@ -35,7 +35,7 @@ ircd::b64encode_unpadded(const const_raw_buffer &in)
/// Allocate and return a string from the encoding of in
std::string
ircd::b64encode(const const_raw_buffer &in)
ircd::b64encode(const const_buffer &in)
{
return _b64encode(in, [](const auto &out, const auto &in)
{
@ -45,7 +45,7 @@ ircd::b64encode(const const_raw_buffer &in)
/// Internal; dedupes encoding functions that create and return a string
static std::string
ircd::_b64encode(const const_raw_buffer &in,
ircd::_b64encode(const const_buffer &in,
const _b64_encoder &encoder)
{
// Allocate a buffer 1.33 times larger than input with pessimistic
@ -66,7 +66,7 @@ ircd::_b64encode(const const_raw_buffer &in,
/// padding is not present in the returned view.
ircd::string_view
ircd::b64encode(const mutable_buffer &out,
const const_raw_buffer &in)
const const_buffer &in)
{
const auto pads
{
@ -92,7 +92,7 @@ ircd::b64encode(const mutable_buffer &out,
/// Encoding in to base64 at out. Out must be 1.33+ larger than in.
ircd::string_view
ircd::b64encode_unpadded(const mutable_buffer &out,
const const_raw_buffer &in)
const const_buffer &in)
{
namespace iterators = boost::archive::iterators;
using transform = iterators::transform_width<unsigned char *, 6, 8>;
@ -127,9 +127,9 @@ ircd::b64decode(const string_view &in)
};
std::string ret(max, char{});
const mutable_raw_buffer buf
const mutable_buffer buf
{
reinterpret_cast<unsigned char *>(const_cast<char *>(ret.data())), ret.size()
const_cast<char *>(ret.data()), ret.size()
};
const auto decoded
@ -144,8 +144,8 @@ ircd::b64decode(const string_view &in)
/// Decode base64 from in to the buffer at out; out can be 75% of the size
/// of in.
ircd::const_raw_buffer
ircd::b64decode(const mutable_raw_buffer &out,
ircd::const_buffer
ircd::b64decode(const mutable_buffer &out,
const string_view &in)
{
namespace iterators = boost::archive::iterators;
@ -183,9 +183,9 @@ std::string
ircd::b58decode(const string_view &in)
{
std::string ret(b58decode_size(in), char{});
const mutable_raw_buffer buf
const mutable_buffer buf
{
reinterpret_cast<unsigned char *>(const_cast<char *>(ret.data())), ret.size()
const_cast<char *>(ret.data()), ret.size()
};
const auto decoded
@ -198,8 +198,8 @@ ircd::b58decode(const string_view &in)
return ret;
}
ircd::const_raw_buffer
ircd::b58decode(const mutable_raw_buffer &buf,
ircd::const_buffer
ircd::b58decode(const mutable_buffer &buf,
const string_view &in)
{
auto p(begin(in));
@ -207,7 +207,7 @@ ircd::b58decode(const mutable_raw_buffer &buf,
for(; p != end(in) && *p == '1'; ++p)
++zeroes;
const mutable_raw_buffer out
const mutable_buffer out
{
data(buf) + zeroes, std::min(b58decode_size(in), size(buf) - zeroes)
};
@ -238,7 +238,7 @@ ircd::b58decode(const mutable_raw_buffer &buf,
}
std::string
ircd::b58encode(const const_raw_buffer &in)
ircd::b58encode(const const_buffer &in)
{
return string(b58encode_size(in), [&in]
(const mutable_buffer &buf)
@ -249,7 +249,7 @@ ircd::b58encode(const const_raw_buffer &in)
ircd::string_view
ircd::b58encode(const mutable_buffer &buf,
const const_raw_buffer &in)
const const_buffer &in)
{
auto p(begin(in));
size_t zeroes(0);

View file

@ -3461,39 +3461,22 @@ ircd::db::read(column &column,
return ret;
}
size_t
ircd::db::read(column &column,
const string_view &key,
const mutable_raw_buffer &buf,
const gopts &gopts)
{
size_t ret(0);
const auto copy([&ret, &buf]
(const string_view &src)
{
ret = std::min(size(src), size(buf));
memcpy(data(buf), data(src), ret);
});
column(key, copy, gopts);
return ret;
}
ircd::string_view
ircd::db::read(column &column,
const string_view &key,
const mutable_buffer &buf,
const gopts &gopts)
{
size_t ret(0);
const auto copy([&ret, &buf]
size_t len(0);
const auto copy([&len, &buf]
(const string_view &src)
{
ret = strlcpy(buf, src);
len = std::min(size(src), size(buf));
memcpy(data(buf), data(src), len);
});
column(key, copy, gopts);
return { data(buf), ret };
return { data(buf), len };
}
template<>
@ -3596,7 +3579,7 @@ ircd::db::del(column &column,
void
ircd::db::write(column &column,
const string_view &key,
const mutable_raw_buffer &buf,
const mutable_buffer &buf,
const sopts &sopts)
{
const string_view val

View file

@ -83,7 +83,7 @@ noexcept
namespace ircd::fs
{
string_view read__std(const string_view &path, const mutable_raw_buffer &, const read_opts &);
string_view read__std(const string_view &path, const mutable_buffer &, const read_opts &);
std::string read__std(const string_view &path, const read_opts &);
}
@ -109,7 +109,7 @@ ircd::fs::read(const string_view &path,
ircd::string_view
ircd::fs::read(const string_view &path,
const mutable_raw_buffer &buf,
const mutable_buffer &buf,
const read_opts &opts)
{
#ifdef IRCD_USE_AIO
@ -137,16 +137,16 @@ ircd::fs::read__std(const string_view &path,
ircd::string_view
ircd::fs::read__std(const string_view &path,
const mutable_raw_buffer &buf,
const mutable_buffer &buf,
const read_opts &opts)
{
std::ifstream file{std::string{path}};
file.exceptions(file.failbit | file.badbit);
file.seekg(opts.offset, file.beg);
file.read(reinterpret_cast<char *>(data(buf)), size(buf));
file.read(data(buf), size(buf));
return
{
reinterpret_cast<const char *>(data(buf)), size_t(file.gcount())
data(buf), size_t(file.gcount())
};
}
@ -158,7 +158,7 @@ ircd::fs::read__std(const string_view &path,
bool
ircd::fs::write(const std::string &path,
const const_raw_buffer &buf)
const const_buffer &buf)
{
if(fs::exists(path))
return false;
@ -168,26 +168,26 @@ ircd::fs::write(const std::string &path,
bool
ircd::fs::overwrite(const string_view &path,
const const_raw_buffer &buf)
const const_buffer &buf)
{
return overwrite(std::string{path}, buf);
}
bool
ircd::fs::overwrite(const std::string &path,
const const_raw_buffer &buf)
const const_buffer &buf)
{
std::ofstream file{path, std::ios::trunc};
file.write(reinterpret_cast<const char *>(data(buf)), size(buf));
file.write(data(buf), size(buf));
return true;
}
bool
ircd::fs::append(const std::string &path,
const const_raw_buffer &buf)
const const_buffer &buf)
{
std::ofstream file{path, std::ios::app};
file.write(reinterpret_cast<const char *>(data(buf)), size(buf));
file.write(data(buf), size(buf));
return true;
}

View file

@ -25,15 +25,15 @@ noexcept
}
ircd::crh::hash &
ircd::crh::hash::operator+=(const const_raw_buffer &buf)
ircd::crh::hash::operator+=(const const_buffer &buf)
{
update(buf);
return *this;
}
void
ircd::crh::hash::operator()(const mutable_raw_buffer &out,
const const_raw_buffer &in)
ircd::crh::hash::operator()(const mutable_buffer &out,
const const_buffer &in)
{
update(in);
finalize(out);

View file

@ -664,7 +664,7 @@ ircd::replace(const string_view &s,
}
std::string
ircd::u2a(const const_raw_buffer &in)
ircd::u2a(const const_buffer &in)
{
return string(size(in) * 2, [&in]
(const mutable_buffer &out)
@ -675,7 +675,7 @@ ircd::u2a(const const_raw_buffer &in)
ircd::string_view
ircd::u2a(const mutable_buffer &out,
const const_raw_buffer &in)
const const_buffer &in)
{
char *p(data(out));
for(size_t i(0); i < size(in); ++i)
@ -684,8 +684,8 @@ ircd::u2a(const mutable_buffer &out,
return { data(out), size_t(p - data(out)) };
}
ircd::const_raw_buffer
ircd::a2u(const mutable_raw_buffer &out,
ircd::const_buffer
ircd::a2u(const mutable_buffer &out,
const const_buffer &in)
{
const size_t len{size(in) / 2};

View file

@ -36,7 +36,7 @@ ircd::m::event_id(const event &event,
ircd::m::id::event
ircd::m::event_id(const event &event,
id::event::buf &buf,
const const_raw_buffer &hash)
const const_buffer &hash)
{
char readable[b58encode_size(sha256::digest_size)];
return id::event

View file

@ -91,7 +91,7 @@ ircd::m::keys::init::certificate()
fs::read(cert_file)
};
const unique_buffer<mutable_raw_buffer> der_buf
const unique_buffer<mutable_buffer> der_buf
{
8_KiB
};
@ -101,7 +101,7 @@ ircd::m::keys::init::certificate()
openssl::cert2d(der_buf, cert_pem)
};
const fixed_buffer<const_raw_buffer, crh::sha256::digest_size> hash
const fixed_buffer<const_buffer, crh::sha256::digest_size> hash
{
sha256{cert_der}
};
@ -139,9 +139,9 @@ ircd::m::keys::init::signing()
};
self::public_key_b64 = b64encode_unpadded(self::public_key);
const fixed_buffer<const_raw_buffer, sha256::digest_size> hash
const fixed_buffer<const_buffer, sha256::digest_size> hash
{
sha256{const_raw_buffer{self::public_key}}
sha256{const_buffer{self::public_key}}
};
const auto public_key_hash_b58
@ -210,7 +210,7 @@ ircd::m::keys::init::bootstrap()
const ed25519::sig sig
{
self::secret_key.sign(const_raw_buffer{presig})
self::secret_key.sign(const_buffer{presig})
};
static char signature[256];
@ -533,7 +533,7 @@ noexcept try
m::keys copy{keys};
at<"signatures"_>(copy) = string_view{};
const json::strung preimage{copy};
return pk.verify(const_raw_buffer{preimage}, sig);
return pk.verify(const_buffer{preimage}, sig);
}
catch(const std::exception &e)
{

View file

@ -431,9 +431,8 @@ ircd::m::user::password(const string_view &password)
try
{
//TODO: ADD SALT
char b64[64];
uint8_t hash[32];
sha256{hash, const_buffer{password}};
char b64[64], hash[32];
sha256{hash, password};
const auto digest{b64encode_unpadded(b64, hash)};
send(accounts, me.user_id, "ircd.password", user_id,
{
@ -460,9 +459,8 @@ const
};
//TODO: ADD SALT
char b64[64];
uint8_t hash[32];
sha256{hash, const_buffer{supplied_password}};
char b64[64], hash[32];
sha256{hash, supplied_password};
const auto supplied_hash{b64encode_unpadded(b64, hash)};
const vm::query<vm::where::test> correct_password{[&supplied_hash]
(const auto &event)

View file

@ -78,7 +78,7 @@ ircd::m::vm::commit(json::iov &event,
};
thread_local char preimage_buf[64_KiB];
const_raw_buffer preimage
const_buffer preimage
{
stringify(mutable_buffer{preimage_buf}, event)
};

View file

@ -56,8 +56,8 @@ ircd::net::log
"net", 'N'
};
ircd::const_raw_buffer
ircd::net::peer_cert_der(const mutable_raw_buffer &buf,
ircd::const_buffer
ircd::net::peer_cert_der(const mutable_buffer &buf,
const socket &socket)
{
const SSL &ssl(socket);
@ -2966,23 +2966,3 @@ const
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
{
data(*this), size(*this)
};
}

View file

@ -433,8 +433,8 @@ ircd::openssl::print(const mutable_buffer &buf,
});
}
ircd::const_raw_buffer
ircd::openssl::cert2d(const mutable_raw_buffer &out,
ircd::const_buffer
ircd::openssl::cert2d(const mutable_buffer &out,
const string_view &pem)
{
const custom_ptr<X509> x509
@ -476,8 +476,8 @@ ircd::openssl::write_pem(const mutable_buffer &out,
});
}
ircd::const_raw_buffer
ircd::openssl::i2d(const mutable_raw_buffer &buf,
ircd::const_buffer
ircd::openssl::i2d(const mutable_buffer &buf,
const X509 &_cert)
{
auto &cert
@ -499,8 +499,8 @@ ircd::openssl::i2d(const mutable_raw_buffer &buf,
"DER requires a %zu byte buffer, you supplied %zu bytes", len, size(buf)
};
uint8_t *out(data(buf));
const const_raw_buffer ret
auto *out(reinterpret_cast<uint8_t *>(data(buf)));
const const_buffer ret
{
data(buf), size_t(i2d_X509(&cert, &out))
};
@ -508,7 +508,7 @@ ircd::openssl::i2d(const mutable_raw_buffer &buf,
if(unlikely(size(ret) != size_t(len)))
throw error();
assert(out - data(buf) == len);
assert(out - reinterpret_cast<uint8_t *>(data(buf)) == len);
return ret;
}
@ -1032,7 +1032,7 @@ ircd::string_view
ircd::openssl::u2a(const mutable_buffer &out,
const BIGNUM *const &a)
{
const unique_buffer<mutable_raw_buffer> tmp
const unique_buffer<mutable_buffer> tmp
{
size(a)
};
@ -1040,8 +1040,8 @@ ircd::openssl::u2a(const mutable_buffer &out,
return ircd::u2a(out, data(tmp, a));
}
ircd::mutable_raw_buffer
ircd::openssl::data(const mutable_raw_buffer &out,
ircd::mutable_buffer
ircd::openssl::data(const mutable_buffer &out,
const BIGNUM *const &a)
{
if(!a)
@ -1055,7 +1055,7 @@ ircd::openssl::data(const mutable_raw_buffer &out,
const auto len
{
BN_bn2bin(a, data(out))
BN_bn2bin(a, reinterpret_cast<uint8_t *>(data(out)))
};
reverse(out);
@ -1076,21 +1076,21 @@ ircd::openssl::size(const BIGNUM *const &a)
ircd::openssl::bignum::bignum(const uint128_t &val)
:bignum
{
const_raw_buffer
const_buffer
{
reinterpret_cast<const uint8_t *>(&val), sizeof(val)
reinterpret_cast<const char *>(&val), sizeof(val)
}
}
{
}
ircd::openssl::bignum::bignum(const const_raw_buffer &bin)
ircd::openssl::bignum::bignum(const const_buffer &bin)
:a{[&bin]
{
// Our binary buffer is little endian. We use
thread_local uint8_t tmp[64_KiB];
// Our binary buffer is little endian.
thread_local char tmp[64_KiB];
const critical_assertion ca;
const mutable_raw_buffer buf{tmp, size(bin)};
const mutable_buffer buf{tmp, size(bin)};
if(unlikely(size(buf) > sizeof(tmp)))
throw buffer_error
{
@ -1098,7 +1098,7 @@ ircd::openssl::bignum::bignum(const const_raw_buffer &bin)
};
reverse(buf, bin);
return BN_bin2bn(data(buf), size(buf), nullptr);
return BN_bin2bn(reinterpret_cast<uint8_t *>(data(buf)), size(buf), nullptr);
}()}
{
if(unlikely(!a))
@ -1155,9 +1155,9 @@ ircd::uint128_t()
const
{
uint128_t ret{0};
const mutable_raw_buffer buf
const mutable_buffer buf
{
reinterpret_cast<uint8_t *>(&ret), sizeof(ret)
reinterpret_cast<char *>(&ret), sizeof(ret)
};
data(buf, a);
@ -1285,7 +1285,7 @@ ircd::openssl::init::~init()
namespace ircd::crh
{
static void finalize(struct sha256::ctx *const &, const mutable_raw_buffer &);
static void finalize(struct sha256::ctx *const &, const mutable_buffer &);
}
struct ircd::crh::sha256::ctx
@ -1311,15 +1311,15 @@ ircd::crh::sha256::sha256()
}
/// One-shot functor. Immediately calls update(); no output
ircd::crh::sha256::sha256(const const_raw_buffer &in)
ircd::crh::sha256::sha256(const const_buffer &in)
:sha256{}
{
update(in);
}
/// One-shot functor. Immediately calls operator().
ircd::crh::sha256::sha256(const mutable_raw_buffer &out,
const const_raw_buffer &in)
ircd::crh::sha256::sha256(const mutable_buffer &out,
const const_buffer &in)
:sha256{}
{
operator()(out, in);
@ -1331,13 +1331,13 @@ noexcept
}
void
ircd::crh::sha256::update(const const_raw_buffer &buf)
ircd::crh::sha256::update(const const_buffer &buf)
{
openssl::call(::SHA256_Update, ctx.get(), data(buf), size(buf));
}
void
ircd::crh::sha256::digest(const mutable_raw_buffer &buf)
ircd::crh::sha256::digest(const mutable_buffer &buf)
const
{
auto copy(*ctx);
@ -1345,7 +1345,7 @@ const
}
void
ircd::crh::sha256::finalize(const mutable_raw_buffer &buf)
ircd::crh::sha256::finalize(const mutable_buffer &buf)
{
crh::finalize(ctx.get(), buf);
}
@ -1359,7 +1359,7 @@ const
void
ircd::crh::finalize(struct sha256::ctx *const &ctx,
const mutable_raw_buffer &buf)
const mutable_buffer &buf)
{
uint8_t *const md
{

View file

@ -64,7 +64,7 @@ noexcept
//
void
ircd::buffer::zero(const mutable_raw_buffer &buf)
ircd::buffer::zero(const mutable_buffer &buf)
{
sodium_memzero(data(buf), size(buf));
}
@ -78,7 +78,7 @@ static_assert(ircd::ed25519::SK_SZ == crypto_sign_ed25519_SECRETKEYBYTES);
static_assert(ircd::ed25519::PK_SZ == crypto_sign_ed25519_PUBLICKEYBYTES);
ircd::ed25519::sk::sk(pk *const &pk_arg,
const const_raw_buffer &seed)
const const_buffer &seed)
:key
{
reinterpret_cast<uint8_t *>(::sodium_malloc(crypto_sign_ed25519_SECRETKEYBYTES)),
@ -92,9 +92,19 @@ ircd::ed25519::sk::sk(pk *const &pk_arg,
pk_arg? *pk_arg : discard
};
const auto pk_data
{
reinterpret_cast<uint8_t *>(pk.data())
};
const auto seed_data
{
reinterpret_cast<const uint8_t *>(data(seed))
};
throw_on_error
{
::crypto_sign_ed25519_seed_keypair(pk.data(), key.get(), data(seed))
::crypto_sign_ed25519_seed_keypair(pk_data, key.get(), seed_data)
};
}
@ -111,9 +121,19 @@ ircd::ed25519::sk::sk(const std::string &filename,
pk_arg? *pk_arg : discard
};
const auto pk_data
{
reinterpret_cast<uint8_t *>(pk.data())
};
const mutable_buffer key_data
{
reinterpret_cast<char *>(key.get()), SK_SZ
};
const auto existing
{
fs::read(filename, mutable_raw_buffer{key.get(), SK_SZ})
fs::read(filename, key_data)
};
if(!existing)
@ -123,29 +143,40 @@ ircd::ed25519::sk::sk(const std::string &filename,
throw_on_error
{
::crypto_sign_ed25519_keypair(pk.data(), key.get())
::crypto_sign_ed25519_keypair(pk_data, key.get())
};
fs::write(filename, const_raw_buffer{key.get(), SK_SZ});
fs::write(filename, key_data);
}
throw_on_error
{
::crypto_sign_ed25519_sk_to_pk(pk.data(), key.get())
::crypto_sign_ed25519_sk_to_pk(pk_data, key.get())
};
}
ircd::ed25519::sig
ircd::ed25519::sk::sign(const const_raw_buffer &msg)
ircd::ed25519::sk::sign(const const_buffer &msg)
const
{
unsigned long long sig_sz;
struct sig sig;
unsigned long long sig_sz;
const auto sig_data
{
reinterpret_cast<uint8_t *>(sig.data())
};
const auto msg_data
{
reinterpret_cast<const uint8_t *>(buffer::data(msg))
};
throw_on_error
{
::crypto_sign_ed25519_detached(sig.data(),
::crypto_sign_ed25519_detached(sig_data,
&sig_sz,
buffer::data(msg),
msg_data,
buffer::size(msg),
key.get())
};
@ -157,16 +188,31 @@ const
}
bool
ircd::ed25519::pk::verify(const const_raw_buffer &msg,
ircd::ed25519::pk::verify(const const_buffer &msg,
const sig &sig)
const
{
const auto sig_data
{
reinterpret_cast<const uint8_t *>(sig.data())
};
const auto msg_data
{
reinterpret_cast<const uint8_t *>(buffer::data(msg))
};
const auto key_data
{
reinterpret_cast<const uint8_t *>(data())
};
const int ret
{
::crypto_sign_ed25519_verify_detached(sig.data(),
buffer::data(msg),
::crypto_sign_ed25519_verify_detached(sig_data,
msg_data,
buffer::size(msg),
data())
key_data)
};
return ret == 0? true:

View file

@ -73,7 +73,7 @@ void foop()
{
using namespace ircd;
uint8_t seed_buf[ed25519::SEED_SZ + 10];
char seed_buf[ed25519::SEED_SZ + 10];
const auto seed
{
b64decode(seed_buf, "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA1")
@ -89,7 +89,7 @@ void foop()
{
const auto sig
{
sk.sign(const_raw_buffer{object})
sk.sign(const_buffer{object})
};
char sigb64_buf[128];
@ -105,7 +105,7 @@ void foop()
b64decode(unsig, sigb64)
};
return pk.verify(const_raw_buffer{object}, unsig);
return pk.verify(const_buffer{object}, unsig);
}};
std::cout <<