Options to use lowercase base32 encoding without padding
This commit is contained in:
parent
1d2727ad25
commit
007a580207
|
@ -39,6 +39,19 @@ POSSIBILITY OF SUCH DAMAGE.
|
|||
|
||||
namespace libtorrent
|
||||
{
|
||||
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
|
||||
};
|
||||
|
||||
}
|
||||
TORRENT_EXTRA_EXPORT std::string unescape_string(std::string const& s, error_code& ec);
|
||||
// replaces all disallowed URL characters by their %-encoding
|
||||
TORRENT_EXTRA_EXPORT std::string escape_string(const char* str, int len);
|
||||
|
@ -58,7 +71,7 @@ namespace libtorrent
|
|||
// encodes a string using the base64 scheme
|
||||
TORRENT_EXTRA_EXPORT std::string base64encode(std::string const& s);
|
||||
// encodes a string using the base32 scheme
|
||||
TORRENT_EXTRA_EXPORT std::string base32encode(std::string const& s);
|
||||
TORRENT_EXTRA_EXPORT std::string base32encode(std::string const& s, int flags=0);
|
||||
TORRENT_EXTRA_EXPORT std::string base32decode(std::string const& s);
|
||||
|
||||
TORRENT_EXTRA_EXPORT std::string url_has_argument(
|
||||
|
|
|
@ -321,15 +321,23 @@ namespace libtorrent
|
|||
return ret;
|
||||
}
|
||||
|
||||
std::string base32encode(std::string const& s)
|
||||
std::string base32encode(std::string const& s, int flags)
|
||||
{
|
||||
static const char base32_table[] =
|
||||
static const char base32_table_canonical[] =
|
||||
{
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z', '2', '3', '4', '5', '6', '7'
|
||||
};
|
||||
static const char base32_table_lowercase[] =
|
||||
{
|
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
|
||||
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
|
||||
'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
|
||||
'y', 'z', '2', '3', '4', '5', '6', '7'
|
||||
};
|
||||
const char *base32_table = 0 != (flags & string::lowercase) ? base32_table_lowercase : base32_table_canonical;
|
||||
|
||||
int input_output_mapping[] = {0, 2, 4, 5, 7, 8};
|
||||
|
||||
|
@ -365,10 +373,13 @@ namespace libtorrent
|
|||
ret += base32_table[outbuf[j]];
|
||||
}
|
||||
|
||||
// write pad
|
||||
for (int j = 0; j < 8 - num_out; ++j)
|
||||
if (0 == (flags & string::no_padding))
|
||||
{
|
||||
ret += '=';
|
||||
// write pad
|
||||
for (int j = 0; j < 8 - num_out; ++j)
|
||||
{
|
||||
ret += '=';
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
|
|
|
@ -118,6 +118,11 @@ TORRENT_TEST(string)
|
|||
TEST_CHECK(base32encode("fooba") == "MZXW6YTB");
|
||||
TEST_CHECK(base32encode("foobar") == "MZXW6YTBOI======");
|
||||
|
||||
// base32 for i2p
|
||||
TEST_CHECK(base32encode("fo", string::no_padding) == "MZXQ");
|
||||
TEST_CHECK(base32encode("foob", string::i2p) == "mzxw6yq");
|
||||
TEST_CHECK(base32encode("foobar", string::lowercase) == "mzxw6ytboi======");
|
||||
|
||||
TEST_CHECK(base32decode("") == "");
|
||||
TEST_CHECK(base32decode("MY======") == "f");
|
||||
TEST_CHECK(base32decode("MZXQ====") == "fo");
|
||||
|
|
Loading…
Reference in New Issue