Browse Source

Little improved base64 implementation.

Global variable type changed from dynamic constructed std::string to constant char array.
Truncating conversion eliminated (no MSVC warning).
Unit tests added.
cl-refactor
Paweł Bylica 10 years ago
parent
commit
83a0b93797
  1. 23
      libdevcore/Base64.cpp
  2. 1
      libdevcore/Base64.h
  3. 27
      test/libdevcrypto/hexPrefix.cpp

23
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 <iostream>
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<byte>(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);

1
libdevcore/Base64.h

@ -28,7 +28,6 @@
/// DEVified by Gav Wood.
#pragma once
#include <vector>
#include <string>
#include "Common.h"
#include "FixedHash.h"

27
test/libdevcrypto/hexPrefix.cpp

@ -27,6 +27,7 @@
#include "../JsonSpiritHeaders.h"
#include <libdevcore/Log.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/Base64.h>
#include <libdevcrypto/TrieCommon.h>
#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()

Loading…
Cancel
Save