make base32encode flags type safe

This commit is contained in:
arvidn 2017-07-26 08:08:25 -07:00 committed by Arvid Norberg
parent b38efb6c65
commit cd343c91bc
2 changed files with 15 additions and 14 deletions

View File

@ -37,21 +37,22 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/string_view.hpp"
#include "libtorrent/flags.hpp"
namespace libtorrent {
// hidden
struct encode_string_flags_tag;
using encode_string_flags_t = flags::bitfield_flag<std::uint8_t, encode_string_flags_tag>;
namespace string
{
enum flags_t
{
// use lower case alphabet used with i2p
lowercase = 0x1,
// don't insert padding
no_padding = 0x2,
// shortcut used for addresses as sha256 hashes
i2p = lowercase | no_padding
};
// use lower case alphabet used with i2p
constexpr encode_string_flags_t lowercase{0x1};
// don't insert padding
constexpr encode_string_flags_t no_padding{0x2};
// shortcut used for addresses as sha256 hashes
constexpr encode_string_flags_t i2p = lowercase | no_padding;
}
TORRENT_EXTRA_EXPORT std::string unescape_string(string_view s, error_code& ec);
@ -80,7 +81,7 @@ namespace libtorrent {
TORRENT_EXTRA_EXPORT std::string base64encode(std::string const& s);
#if TORRENT_USE_I2P
// encodes a string using the base32 scheme
TORRENT_EXTRA_EXPORT std::string base32encode(string_view s, int flags = 0);
TORRENT_EXTRA_EXPORT std::string base32encode(string_view s, encode_string_flags_t flags = {});
#endif
TORRENT_EXTRA_EXPORT std::string base32decode(string_view s);

View File

@ -340,7 +340,7 @@ namespace libtorrent {
}
#if TORRENT_USE_I2P
std::string base32encode(string_view s, int flags)
std::string base32encode(string_view s, encode_string_flags_t const flags)
{
static char const base32_table_canonical[] =
{
@ -356,7 +356,7 @@ namespace libtorrent {
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '2', '3', '4', '5', '6', '7'
};
char const *base32_table = 0 != (flags & string::lowercase) ? base32_table_lowercase : base32_table_canonical;
char const *base32_table = (flags & string::lowercase) ? base32_table_lowercase : base32_table_canonical;
static aux::array<int, 6> const input_output_mapping{{{0, 2, 4, 5, 7, 8}}};
@ -392,7 +392,7 @@ namespace libtorrent {
ret += base32_table[outbuf[j]];
}
if (0 == (flags & string::no_padding))
if (!(flags & string::no_padding))
{
// write pad
for (int j = 0; j < int(outbuf.size()) - num_out; ++j)