mirror of
https://github.com/matrix-construct/construct
synced 2025-01-15 17:16:49 +01:00
ircd::buffer: Add raw_buffer for binary data; lexical: simplify a2u/b64 related.
This commit is contained in:
parent
d7def063b6
commit
4b17211a04
4 changed files with 57 additions and 61 deletions
|
@ -48,9 +48,14 @@ namespace ircd::buffer
|
||||||
template<class it> struct buffer;
|
template<class it> struct buffer;
|
||||||
struct const_buffer;
|
struct const_buffer;
|
||||||
struct mutable_buffer;
|
struct mutable_buffer;
|
||||||
|
struct const_raw_buffer;
|
||||||
|
struct mutable_raw_buffer;
|
||||||
template<class buffer, size_t align> struct unique_buffer;
|
template<class buffer, size_t align> struct unique_buffer;
|
||||||
|
|
||||||
template<template<class> class I> using const_buffers = I<const_buffer>;
|
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 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
|
// Single buffer iteration of contents
|
||||||
template<class it> const it &begin(const buffer<it> &buffer);
|
template<class it> const it &begin(const buffer<it> &buffer);
|
||||||
|
@ -87,6 +92,8 @@ namespace ircd
|
||||||
{
|
{
|
||||||
using buffer::const_buffer;
|
using buffer::const_buffer;
|
||||||
using buffer::mutable_buffer;
|
using buffer::mutable_buffer;
|
||||||
|
using buffer::const_raw_buffer;
|
||||||
|
using buffer::mutable_raw_buffer;
|
||||||
using buffer::unique_buffer;
|
using buffer::unique_buffer;
|
||||||
using buffer::null_buffer;
|
using buffer::null_buffer;
|
||||||
|
|
||||||
|
@ -146,8 +153,6 @@ struct ircd::buffer::const_buffer
|
||||||
const_buffer(const string_view &s)
|
const_buffer(const string_view &s)
|
||||||
:buffer<const char *>{std::begin(s), std::end(s)}
|
:buffer<const char *>{std::begin(s), std::end(s)}
|
||||||
{}
|
{}
|
||||||
|
|
||||||
const_buffer() = default;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ircd::buffer::mutable_buffer
|
struct ircd::buffer::mutable_buffer
|
||||||
|
@ -158,6 +163,22 @@ struct ircd::buffer::mutable_buffer
|
||||||
using buffer<char *>::buffer;
|
using buffer<char *>::buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ircd::buffer::const_raw_buffer
|
||||||
|
:buffer<const unsigned char *>
|
||||||
|
{
|
||||||
|
operator boost::asio::const_buffer() const;
|
||||||
|
|
||||||
|
using buffer<const unsigned char *>::buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ircd::buffer::mutable_raw_buffer
|
||||||
|
:buffer<unsigned char *>
|
||||||
|
{
|
||||||
|
operator boost::asio::mutable_buffer() const;
|
||||||
|
|
||||||
|
using buffer<unsigned char *>::buffer;
|
||||||
|
};
|
||||||
|
|
||||||
template<template<class>
|
template<template<class>
|
||||||
class buffers,
|
class buffers,
|
||||||
class T>
|
class T>
|
||||||
|
|
|
@ -91,17 +91,9 @@ namespace ircd
|
||||||
// Binary / Hex / Base64 conversion suite
|
// 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 const_raw_buffer &in);
|
||||||
string_view u2a(const mutable_buffer &out, const uint8_t *const &in, const size_t &len);
|
const_raw_buffer a2u(const mutable_raw_buffer &out, const const_buffer &in);
|
||||||
string_view u2a(const mutable_buffer &out, const const_buffer &in);
|
string_view b64encode(const mutable_buffer &out, const const_raw_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 tokenization.
|
// String tokenization.
|
||||||
|
|
|
@ -237,20 +237,7 @@ ircd::tokens(const string_view &str,
|
||||||
|
|
||||||
ircd::string_view
|
ircd::string_view
|
||||||
ircd::b64encode(const mutable_buffer &out,
|
ircd::b64encode(const mutable_buffer &out,
|
||||||
const const_buffer &in)
|
const const_raw_buffer &in)
|
||||||
{
|
|
||||||
const auto ptr
|
|
||||||
{
|
|
||||||
reinterpret_cast<const uint8_t *>(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)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
using transform = boost::archive::iterators::transform_width<unsigned char *, 6, 8>;
|
using transform = boost::archive::iterators::transform_width<unsigned char *, 6, 8>;
|
||||||
|
@ -258,26 +245,13 @@ ircd::b64encode(const mutable_buffer &out,
|
||||||
using ostream_iterator = boost::archive::iterators::ostream_iterator<char>;
|
using ostream_iterator = boost::archive::iterators::ostream_iterator<char>;
|
||||||
|
|
||||||
std::stringstream ss;
|
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)));
|
const auto outlen(ss.str().copy(data(out), size(out)));
|
||||||
return { data(out), outlen };
|
return { data(out), outlen };
|
||||||
}
|
}
|
||||||
|
|
||||||
ircd::const_buffer
|
ircd::const_raw_buffer
|
||||||
ircd::a2u(const mutable_buffer &out,
|
ircd::a2u(const mutable_raw_buffer &out,
|
||||||
const const_buffer &in)
|
|
||||||
{
|
|
||||||
const auto ptr
|
|
||||||
{
|
|
||||||
reinterpret_cast<uint8_t *>(data(out))
|
|
||||||
};
|
|
||||||
|
|
||||||
return a2u(ptr, size(out), in);
|
|
||||||
}
|
|
||||||
|
|
||||||
ircd::const_buffer
|
|
||||||
ircd::a2u(uint8_t *const &out,
|
|
||||||
const size_t &max,
|
|
||||||
const const_buffer &in)
|
const const_buffer &in)
|
||||||
{
|
{
|
||||||
const size_t len{size(in) / 2};
|
const size_t len{size(in) / 2};
|
||||||
|
@ -293,28 +267,15 @@ ircd::a2u(uint8_t *const &out,
|
||||||
out[i] = strtol(gl, nullptr, 16);
|
out[i] = strtol(gl, nullptr, 16);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { reinterpret_cast<const char *>(out), len };
|
return { data(out), len };
|
||||||
}
|
}
|
||||||
|
|
||||||
ircd::string_view
|
ircd::string_view
|
||||||
ircd::u2a(const mutable_buffer &out,
|
ircd::u2a(const mutable_buffer &out,
|
||||||
const const_buffer &in)
|
const const_raw_buffer &in)
|
||||||
{
|
|
||||||
const auto ptr
|
|
||||||
{
|
|
||||||
reinterpret_cast<const uint8_t *>(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)
|
|
||||||
{
|
{
|
||||||
char *p(data(out));
|
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]);
|
p += snprintf(p, size(out) - (p - data(out)), "%02x", in[i]);
|
||||||
|
|
||||||
return { data(out), size_t(p - data(out)) };
|
return { data(out), size_t(p - data(out)) };
|
||||||
|
|
|
@ -520,7 +520,8 @@ ircd::buffer::null_buffers
|
||||||
null_buffer
|
null_buffer
|
||||||
}};
|
}};
|
||||||
|
|
||||||
ircd::buffer::mutable_buffer::operator boost::asio::mutable_buffer()
|
ircd::buffer::mutable_buffer::operator
|
||||||
|
boost::asio::mutable_buffer()
|
||||||
const
|
const
|
||||||
{
|
{
|
||||||
return boost::asio::mutable_buffer
|
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
|
const
|
||||||
{
|
{
|
||||||
return boost::asio::const_buffer
|
return boost::asio::const_buffer
|
||||||
|
|
Loading…
Reference in a new issue