Browse Source

Merge pull request #1951 from imapp-pl/no_warnings

Little improved base64 implementation.
cl-refactor
Gav Wood 10 years ago
parent
commit
e93fd742e0
  1. 35
      libdevcore/Base64.cpp
  2. 1
      libdevcore/Base64.h
  3. 39
      test/libdevcrypto/hexPrefix.cpp

35
libdevcore/Base64.cpp

@ -27,20 +27,27 @@
/// 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 =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static inline bool is_base64(byte c) {
return (isalnum(c) || (c == '+') || (c == '/'));
}
static inline byte find_base64_char_index(byte c) {
if ('A' <= c && c <= 'Z') return c - 'A';
else if ('a' <= c && c <= 'z') return c - 'a' + 1 + find_base64_char_index('Z');
else if ('0' <= c && c <= '9') return c - '0' + 1 + find_base64_char_index('z');
else if (c == '+') return 1 + find_base64_char_index('9');
else if (c == '/') return 1 + find_base64_char_index('+');
else return 1 + find_base64_char_index('/');
}
std::string dev::toBase64(bytesConstRef _in) {
static const char base64_chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string ret;
int i = 0;
int j = 0;
@ -85,7 +92,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 +101,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 +116,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"

39
test/libdevcrypto/hexPrefix.cpp

@ -27,6 +27,7 @@
#include "../JsonSpiritHeaders.h"
#include <libdevcore/Log.h>
#include <libdevcore/CommonIO.h>
#include <libdevcore/Base64.h>
#include <libdevcore/TrieCommon.h>
#include "../TestHelper.h"
@ -60,4 +61,42 @@ 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"},
{
"So?<p>"
"This 4, 5, 6, 7, 8, 9, z, {, |, } tests Base64 encoder. "
"Show me: @, 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, [, \\, ], ^, _, `, "
"a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s.",
"U28/PHA+VGhpcyA0LCA1LCA2LCA3LCA4LCA5LCB6LCB7LCB8LCB9IHRlc3RzIEJhc2U2NCBlbmNv"
"ZGVyLiBTaG93IG1lOiBALCBBLCBCLCBDLCBELCBFLCBGLCBHLCBILCBJLCBKLCBLLCBMLCBNLCBO"
"LCBPLCBQLCBRLCBSLCBTLCBULCBVLCBWLCBXLCBYLCBZLCBaLCBbLCBcLCBdLCBeLCBfLCBgLCBh"
"LCBiLCBjLCBkLCBlLCBmLCBnLCBoLCBpLCBqLCBrLCBsLCBtLCBuLCBvLCBwLCBxLCByLCBzLg=="
}
};
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