From 2d10ac2c788eb6c18357004cfc66148c177bad01 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 3 Mar 2023 22:20:27 -0800 Subject: [PATCH] ircd::b64: De-template dictionary argument; simplify. --- include/ircd/b64.h | 10 +++------- ircd/b64.cc | 36 +++++++++++++++--------------------- matrix/event.cc | 4 ++-- matrix/id.cc | 2 +- matrix/media.cc | 2 +- matrix/user_filter.cc | 2 +- 6 files changed, 23 insertions(+), 33 deletions(-) diff --git a/include/ircd/b64.h b/include/ircd/b64.h index 1237b9b16..cd4a420db 100644 --- a/include/ircd/b64.h +++ b/include/ircd/b64.h @@ -37,13 +37,9 @@ namespace ircd::b64 size_t encode_size(const const_buffer &in) noexcept; size_t encode_unpadded_size(const const_buffer &in) noexcept; - const_buffer decode(const mutable_buffer &out, const string_view &in); - - template - string_view encode(const mutable_buffer &out, const const_buffer &in) noexcept; - - template - string_view encode_unpadded(const mutable_buffer &out, const const_buffer &in) noexcept; + const_buffer decode(const mutable_buffer out, const string_view in); + string_view encode_unpadded(const mutable_buffer out, const const_buffer in, const dictionary & = dict_rfc1421) noexcept; + string_view encode(const mutable_buffer out, const const_buffer in, const dictionary & = dict_rfc1421) noexcept; } inline size_t diff --git a/ircd/b64.cc b/ircd/b64.cc index d6f288a83..846721270 100644 --- a/ircd/b64.cc +++ b/ircd/b64.cc @@ -25,10 +25,8 @@ namespace ircd::b64 extern const i32 decode_tab[256]; - static u8x64 decode_block(const u8x64 block, i64x8 &err) noexcept; - - template - static u8x64 encode_block(const u8x64 block) noexcept; + static u8x64 decode_block(const u8x64 block, i64x8 &__restrict__ err) noexcept; + static u8x64 encode_block(const u8x64 block, const dictionary &) noexcept; } #pragma GCC visibility pop @@ -153,10 +151,10 @@ alignas(64) /// Encoding in to base64 at out. Out must be 1.33+ larger than in /// padding is not present in the returned view. -template ircd::string_view -ircd::b64::encode(const mutable_buffer &out, - const const_buffer &in) +ircd::b64::encode(const mutable_buffer out, + const const_buffer in, + const dictionary &dict) noexcept { const auto pads @@ -166,7 +164,7 @@ noexcept const auto encoded { - encode_unpadded(out, in) + encode_unpadded(out, in, dict) }; const char _pad[2] @@ -187,14 +185,12 @@ noexcept data(out), len }; } -template ircd::string_view ircd::b64::encode(const mutable_buffer &, const const_buffer &) noexcept; -template ircd::string_view ircd::b64::encode(const mutable_buffer &, const const_buffer &) noexcept; /// Encoding in to base64 at out. Out must be 1.33+ larger than in. -template ircd::string_view -ircd::b64::encode_unpadded(const mutable_buffer &out, - const const_buffer &in) +ircd::b64::encode_unpadded(const mutable_buffer out, + const const_buffer in, + const dictionary &dict) noexcept { char *const __restrict__ dst @@ -234,7 +230,7 @@ noexcept }; block = *si; - block = encode_block(block); + block = encode_block(block, dict); *di = block; } @@ -246,7 +242,7 @@ noexcept for(j = 0; j < 48 && i * 48 + j < size(in); ++j) block[j] = src[i * 48 + j]; - block = encode_block(block); + block = encode_block(block, dict); for(j = 0; j < 64 && i * 64 + j < out_len; ++j) dst[i * 64 + j] = block[j]; } @@ -256,8 +252,6 @@ noexcept data(out), out_len }; } -template ircd::string_view ircd::b64::encode_unpadded(const mutable_buffer &, const const_buffer &) noexcept; -template ircd::string_view ircd::b64::encode_unpadded(const mutable_buffer &, const const_buffer &) noexcept; /// Returns 64 base64-encoded characters from 48 input characters. For any /// inputs less than 48 characters trail with null characters; caller computes @@ -270,9 +264,9 @@ template ircd::string_view ircd::b64::encode_unpadded(c /// Based on https://arxiv.org/pdf/1910.05109 (and earlier work). No specific /// intrinsics are used here; instead we recite a kotodama divination known /// as "vector extensions" which by chance is visible to humans as C syntax. -template ircd::u8x64 -ircd::b64::encode_block(const u8x64 in) +ircd::b64::encode_block(const u8x64 in, + const dictionary &dict) noexcept { size_t i, j, k; @@ -318,8 +312,8 @@ noexcept /// Decode base64 from in to the buffer at out; out can be 75% of the size /// of in. ircd::const_buffer -ircd::b64::decode(const mutable_buffer &out, - const string_view &in) +ircd::b64::decode(const mutable_buffer out, + const string_view in) { char *const __restrict__ dst { diff --git a/matrix/event.cc b/matrix/event.cc index dd94c0b8b..bde8354e1 100644 --- a/matrix/event.cc +++ b/matrix/event.cc @@ -118,7 +118,7 @@ ircd::m::make_id(const event &event, { const id::event ret { - buf, b64::encode_unpadded(readable, hash), at<"origin"_>(event) + buf, b64::encode_unpadded(readable, hash, b64::urlsafe), at<"origin"_>(event) }; buf.assigned(ret); @@ -137,7 +137,7 @@ ircd::m::make_id(const event &event, const id::event ret { - buf, b64::encode_unpadded(readable, hash), string_view{} + buf, b64::encode_unpadded(readable, hash, b64::urlsafe), string_view{} }; buf.assigned(ret); diff --git a/matrix/id.cc b/matrix/id.cc index 3ffdfe4a6..095bbd43d 100644 --- a/matrix/id.cc +++ b/matrix/id.cc @@ -927,7 +927,7 @@ ircd::m::id::event::v4::v4(const mutable_buffer &out, out[0] = '$'; const string_view hashb64 { - b64::encode_unpadded(out + 1, hash) + b64::encode_unpadded(out + 1, hash, b64::urlsafe) }; return string_view diff --git a/matrix/media.cc b/matrix/media.cc index 313b069af..5e5bfb6eb 100644 --- a/matrix/media.cc +++ b/matrix/media.cc @@ -468,7 +468,7 @@ ircd::m::media::file::room_id(room::id::buf &out, out = { - b64::encode_unpadded(buf, hash), my_host() + b64::encode_unpadded(buf, hash, b64::urlsafe), my_host() }; return out; diff --git a/matrix/user_filter.cc b/matrix/user_filter.cc index 8025d0234..224d23fef 100644 --- a/matrix/user_filter.cc +++ b/matrix/user_filter.cc @@ -33,7 +33,7 @@ ircd::m::user::filter::set(const mutable_buffer &idbuf, const string_view filter_id { - b64::encode_unpadded(idbuf, hash) + b64::encode_unpadded(idbuf, hash, b64::urlsafe) }; //TODO: ABA