diff --git a/libdevcore/Base64.cpp b/libdevcore/Base64.cpp index 684556cd5..cb6e873df 100644 --- a/libdevcore/Base64.cpp +++ b/libdevcore/Base64.cpp @@ -27,11 +27,9 @@ /// Originally by René Nyffenegger, modified by some other guy and then devified by Gav Wood. #include "Base64.h" -#include -using namespace std; using namespace dev; -static const std::string base64_chars = +static const char base64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; @@ -40,6 +38,11 @@ static inline bool is_base64(byte c) { return (isalnum(c) || (c == '+') || (c == '/')); } +static inline byte find_base64_char_index(byte c) { + auto it = std::find(base64_chars, base64_chars + sizeof(base64_chars), c); + return static_cast(it - base64_chars); +} + std::string dev::toBase64(bytesConstRef _in) { std::string ret; int i = 0; @@ -85,7 +88,7 @@ std::string dev::toBase64(bytesConstRef _in) { } bytes dev::fromBase64(std::string const& encoded_string) { - int in_len = encoded_string.size(); + auto in_len = encoded_string.size(); int i = 0; int j = 0; int in_ = 0; @@ -94,9 +97,9 @@ bytes dev::fromBase64(std::string const& encoded_string) { while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { char_array_4[i++] = encoded_string[in_]; in_++; - if (i ==4) { - for (i = 0; i <4; i++) - char_array_4[i] = base64_chars.find(char_array_4[i]); + if (i == 4) { + for (i = 0; i < 4; i++) + char_array_4[i] = find_base64_char_index(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); @@ -109,11 +112,11 @@ bytes dev::fromBase64(std::string const& encoded_string) { } if (i) { - for (j = i; j <4; j++) + for (j = i; j < 4; j++) char_array_4[j] = 0; - for (j = 0; j <4; j++) - char_array_4[j] = base64_chars.find(char_array_4[j]); + for (j = 0; j < 4; j++) + char_array_4[j] = find_base64_char_index(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); diff --git a/libdevcore/Base64.h b/libdevcore/Base64.h index 8ff10cff0..d99a29767 100644 --- a/libdevcore/Base64.h +++ b/libdevcore/Base64.h @@ -28,7 +28,6 @@ /// DEVified by Gav Wood. #pragma once -#include #include #include "Common.h" #include "FixedHash.h" diff --git a/test/libdevcrypto/hexPrefix.cpp b/test/libdevcrypto/hexPrefix.cpp index c72f24535..c8eff797b 100644 --- a/test/libdevcrypto/hexPrefix.cpp +++ b/test/libdevcrypto/hexPrefix.cpp @@ -27,6 +27,7 @@ #include "../JsonSpiritHeaders.h" #include #include +#include #include #include "../TestHelper.h" @@ -60,4 +61,30 @@ BOOST_AUTO_TEST_CASE(hexPrefix_test) } } +BOOST_AUTO_TEST_CASE(base64) +{ + static char const* const s_tests[][2] = { + {"", ""}, + {"f", "Zg=="}, + {"fo", "Zm8="}, + {"foo", "Zm9v"}, + {"foob", "Zm9vYg=="}, + {"fooba", "Zm9vYmE="}, + {"foobar", "Zm9vYmFy"} + }; + static const auto c_numTests = sizeof(s_tests) / sizeof(s_tests[0]); + + for (size_t i = 0; i < c_numTests; ++i) + { + auto expectedDecoded = std::string{s_tests[i][0]}; + auto expectedEncoded = std::string{s_tests[i][1]}; + + auto encoded = toBase64(expectedDecoded); + BOOST_CHECK_EQUAL(expectedEncoded, encoded); + auto decodedBytes = fromBase64(expectedEncoded); + auto decoded = bytesConstRef{decodedBytes.data(), decodedBytes.size()}.toString(); + BOOST_CHECK_EQUAL(decoded, expectedDecoded); + } +} + BOOST_AUTO_TEST_SUITE_END()