2020-08-10 12:51:09 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_B64_H
|
|
|
|
|
|
|
|
namespace ircd::b64
|
|
|
|
{
|
2020-08-12 02:13:13 +02:00
|
|
|
IRCD_EXCEPTION(ircd::error, error)
|
|
|
|
IRCD_EXCEPTION(error, invalid_encoding)
|
|
|
|
|
2020-08-11 23:19:32 +02:00
|
|
|
using dictionary_element = int;
|
2020-08-12 11:55:35 +02:00
|
|
|
using dictionary = dictionary_element[64];
|
2020-08-11 23:13:16 +02:00
|
|
|
|
|
|
|
extern const dictionary
|
2020-08-12 11:55:35 +02:00
|
|
|
dict_rfc1421, // [62] = '+', [63] = '/'
|
|
|
|
dict_rfc3501, // [62] = '+', [63] = ','
|
|
|
|
dict_rfc4648; // [62] = '-', [63] = '_'
|
2020-08-10 12:51:09 +02:00
|
|
|
|
2020-08-10 14:14:43 +02:00
|
|
|
static const auto
|
|
|
|
&standard { dict_rfc1421 },
|
|
|
|
&mailbox { dict_rfc3501 },
|
|
|
|
&urlsafe { dict_rfc4648 };
|
2020-08-10 12:51:09 +02:00
|
|
|
|
2020-08-10 14:14:43 +02:00
|
|
|
constexpr size_t decode_size(const size_t &) noexcept;
|
|
|
|
constexpr size_t encode_size(const size_t &) noexcept;
|
2020-08-10 12:51:09 +02:00
|
|
|
constexpr size_t encode_unpadded_size(const size_t &) noexcept;
|
|
|
|
|
|
|
|
size_t decode_size(const string_view &in) noexcept;
|
2020-08-10 14:14:43 +02:00
|
|
|
size_t encode_size(const const_buffer &in) noexcept;
|
|
|
|
size_t encode_unpadded_size(const const_buffer &in) noexcept;
|
|
|
|
|
2020-08-10 12:51:09 +02:00
|
|
|
const_buffer decode(const mutable_buffer &out, const string_view &in);
|
|
|
|
|
2020-08-12 11:31:01 +02:00
|
|
|
template<const dictionary & = dict_rfc1421>
|
2020-08-10 14:14:43 +02:00
|
|
|
string_view encode(const mutable_buffer &out, const const_buffer &in) noexcept;
|
|
|
|
|
2020-08-12 11:31:01 +02:00
|
|
|
template<const dictionary & = dict_rfc1421>
|
2020-08-10 14:14:43 +02:00
|
|
|
string_view encode_unpadded(const mutable_buffer &out, const const_buffer &in) noexcept;
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
2020-08-10 14:14:43 +02:00
|
|
|
ircd::b64::encode_unpadded_size(const const_buffer &in)
|
2020-08-10 12:51:09 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-08-10 14:14:43 +02:00
|
|
|
return encode_unpadded_size(size(in));
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
2020-08-10 14:14:43 +02:00
|
|
|
inline size_t
|
|
|
|
ircd::b64::encode_size(const const_buffer &in)
|
2020-08-10 12:51:09 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-08-10 14:14:43 +02:00
|
|
|
return encode_size(size(in));
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
2020-08-10 14:14:43 +02:00
|
|
|
ircd::b64::decode_size(const string_view &in)
|
2020-08-10 12:51:09 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-08-12 01:02:56 +02:00
|
|
|
const size_t pads
|
|
|
|
{
|
|
|
|
endswith_count(in, '=')
|
|
|
|
};
|
|
|
|
|
|
|
|
return decode_size(size(in) - pads);
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t
|
|
|
|
ircd::b64::encode_unpadded_size(const size_t &in)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
return (in * (4.0 / 3.0)) + 1; //XXX: constexpr ceil()
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:14:43 +02:00
|
|
|
constexpr size_t
|
|
|
|
ircd::b64::encode_size(const size_t &in)
|
2020-08-10 12:51:09 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-08-10 14:14:43 +02:00
|
|
|
return ((in * (4.0 / 3.0)) + 1) + (3 - in % 3) % 3; //XXX: constexpr ceil
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t
|
2020-08-10 14:14:43 +02:00
|
|
|
ircd::b64::decode_size(const size_t &in)
|
2020-08-10 12:51:09 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2020-08-10 18:00:20 +02:00
|
|
|
return in * (3.0 / 4.0);
|
2020-08-10 12:51:09 +02:00
|
|
|
}
|