0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-01 18:34:18 +01:00

ircd::base: Remove allocated string convenience overloads; simplify interface.

This commit is contained in:
Jason Volk 2020-08-09 01:37:30 -07:00
parent 8a3f060387
commit 4a2adc4123
4 changed files with 6 additions and 103 deletions

View file

@ -17,31 +17,26 @@ namespace ircd
constexpr size_t b58encode_size(const size_t &);
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_buffer b58decode(const mutable_buffer &out, const string_view &in);
std::string b58decode(const string_view &in);
// Binary -> Base64 conversion suite
constexpr size_t b64encode_size(const size_t &);
size_t b64encode_size(const const_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
constexpr size_t b64encode_unpadded_size(const size_t &);
size_t b64encode_unpadded_size(const const_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)
constexpr size_t b64decode_size(const size_t &);
size_t b64decode_size(const string_view &in);
const_buffer b64decode(const mutable_buffer &out, const string_view &in);
std::string b64decode(const string_view &in);
// Base64 convenience conversions
string_view b64tob58(const mutable_buffer &out, const string_view &in);

View file

@ -88,8 +88,6 @@ ircd::b64tob58(const mutable_buffer &out,
namespace ircd::base
{
using _b64_encoder = std::function<string_view (const mutable_buffer &, const const_buffer &)>;
constexpr char _b64_pad_
{
'='
@ -101,7 +99,6 @@ namespace ircd::base
b64_encode_permute_tab[64];
static u8x64 b64encode(const u8x64 in) noexcept;
static std::string _b64encode(const const_buffer &in, const _b64_encoder &);
}
// [00] - [25] => A - Z
@ -143,45 +140,6 @@ ircd::base::b64_encode_permute_tab
45 + 1, 45 + 0, 45 + 2, 45 + 1,
};
/// Allocate and return a string without padding from the encoding of in
std::string
ircd::b64encode_unpadded(const const_buffer &in)
{
return base::_b64encode(in, [](const auto &out, const auto &in)
{
return b64encode_unpadded(out, in);
});
}
/// Allocate and return a string from the encoding of in
std::string
ircd::b64encode(const const_buffer &in)
{
return base::_b64encode(in, [](const auto &out, const auto &in)
{
return b64encode(out, in);
});
}
/// Internal; dedupes encoding functions that create and return a string
static std::string
ircd::base::_b64encode(const const_buffer &in,
const _b64_encoder &encoder)
{
// Allocate a buffer 1.33 times larger than input with pessimistic
// extra space for any padding and nulling.
const auto max
{
ceil(size(in) * (4.0 / 3.0)) + 4
};
return string(max, [&in, &encoder]
(const mutable_buffer &buf)
{
return encoder(buf, in);
});
}
/// Encoding in to base64 at out. Out must be 1.33+ larger than in
/// padding is not present in the returned view.
ircd::string_view
@ -313,31 +271,6 @@ noexcept
// Base64 decode
//
std::string
ircd::b64decode(const string_view &in)
{
// Allocate a buffer 75% than input with pessimistic extra space
const auto max
{
ceil(size(in) * 0.75) + 4
};
std::string ret(max, char{});
const mutable_buffer buf
{
const_cast<char *>(ret.data()), ret.size()
};
const auto decoded
{
b64decode(buf, in)
};
assert(size(decoded) <= ret.size());
ret.resize(size(decoded));
return ret;
}
/// Decode base64 from in to the buffer at out; out can be 75% of the size
/// of in.
ircd::const_buffer
@ -383,25 +316,6 @@ namespace ircd::base
// Base58 decode
//
std::string
ircd::b58decode(const string_view &in)
{
std::string ret(b58decode_size(in), char{});
const mutable_buffer buf
{
const_cast<char *>(ret.data()), ret.size()
};
const auto decoded
{
b58decode(buf, in)
};
assert(size(decoded) <= ret.size());
ret.resize(size(decoded));
return ret;
}
ircd::const_buffer
ircd::b58decode(const mutable_buffer &buf,
const string_view &in)
@ -445,16 +359,6 @@ ircd::b58decode(const mutable_buffer &buf,
// Base58 encode
//
std::string
ircd::b58encode(const const_buffer &in)
{
return string(b58encode_size(in), [&in]
(const mutable_buffer &buf)
{
return b58encode(buf, in);
});
}
ircd::string_view
ircd::b58encode(const mutable_buffer &buf,
const const_buffer &in)

View file

@ -162,10 +162,13 @@ ircd::m::pretty_detailed(std::ostream &out,
<< std::endl;
if(!verify_hash(event))
{
char buf[512];
out
<< std::setw(9) << std::left << "!!! ERROR" << " "
<< "HASH MISMATCH :" << b64encode_unpadded(hash(event))
<< "HASH MISMATCH :" << b64encode_unpadded(buf, hash(event))
<< std::endl;
}
{
const auto &[authed, failmsg](m::room::auth::check_static(event));

View file

@ -6893,8 +6893,9 @@ console_cmd__stage(opt &out, const string_view &line)
try
{
char buf[512];
if(!verify_hash(event))
out << "- HASH MISMATCH: " << b64encode_unpadded(hash(event)) << std::endl;
out << "- HASH MISMATCH: " << b64encode_unpadded(buf, hash(event)) << std::endl;
}
catch(const std::exception &e)
{