Commit 7f87e0b0 authored by Dominik Charousset's avatar Dominik Charousset

Move encode_base64 function to caf::detail

parent ac0f23bc
...@@ -81,6 +81,7 @@ add_library(libcaf_core_obj OBJECT ${CAF_CORE_HEADERS} ...@@ -81,6 +81,7 @@ add_library(libcaf_core_obj OBJECT ${CAF_CORE_HEADERS}
src/detail/behavior_stack.cpp src/detail/behavior_stack.cpp
src/detail/blocking_behavior.cpp src/detail/blocking_behavior.cpp
src/detail/config_consumer.cpp src/detail/config_consumer.cpp
src/detail/encode_base64.cpp
src/detail/get_mac_addresses.cpp src/detail/get_mac_addresses.cpp
src/detail/get_process_id.cpp src/detail/get_process_id.cpp
src/detail/get_root_uuid.cpp src/detail/get_root_uuid.cpp
...@@ -244,6 +245,7 @@ caf_add_test_suites(caf-core-test ...@@ -244,6 +245,7 @@ caf_add_test_suites(caf-core-test
detached_actors detached_actors
detail.bounds_checker detail.bounds_checker
detail.config_consumer detail.config_consumer
detail.encode_base64
detail.ieee_754 detail.ieee_754
detail.limited_vector detail.limited_vector
detail.meta_object detail.meta_object
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include "caf/byte.hpp"
#include "caf/detail/core_export.hpp"
#include "caf/span.hpp"
#include "caf/string_view.hpp"
#include <string>
namespace caf::detail {
CAF_CORE_EXPORT std::string encode_base64(string_view str);
CAF_CORE_EXPORT std::string encode_base64(span<const byte> bytes);
} // namespace caf::detail
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/encode_base64.hpp"
namespace caf::detail {
namespace {
constexpr const char base64_tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
} // namespace
std::string encode_base64(string_view str) {
auto bytes = make_span(reinterpret_cast<const byte*>(str.data()), str.size());
return encode_base64(bytes);
}
std::string encode_base64(span<const byte> bytes) {
std::string result;
// Consumes three characters from input at once.
auto consume = [&result](const byte* i) {
auto at = [i](size_t index) { return to_integer<int>(i[index]); };
int buf[] = {
(at(0) & 0xfc) >> 2,
((at(0) & 0x03) << 4) + ((at(1) & 0xf0) >> 4),
((at(1) & 0x0f) << 2) + ((at(2) & 0xc0) >> 6),
at(2) & 0x3f,
};
for (auto x : buf)
result += base64_tbl[x];
};
// Iterate the input in chunks of three bytes.
auto i = bytes.begin();
for (; std::distance(i, bytes.end()) >= 3; i += 3)
consume(i);
if (i != bytes.end()) {
// Pad input with zeros.
byte buf[] = {byte{0}, byte{0}, byte{0}};
std::copy(i, bytes.end(), buf);
consume(buf);
// Override padded bytes (garbage) with '='.
for (auto j = result.end() - (3 - (bytes.size() % 3)); j != result.end();
++j)
*j = '=';
}
return result;
}
} // namespace caf::detail
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2020 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE detail.encode_base64
#include "caf/detail/encode_base64.hpp"
#include "caf/test/dsl.hpp"
#include <array>
using namespace caf;
namespace {
template <class... Ts>
auto encode(Ts... xs) {
std::array<byte, sizeof...(Ts)> bytes{{static_cast<byte>(xs)...}};
return detail::encode_base64(bytes);
}
} // namespace
CAF_TEST(base64 encoding converts byte sequences to strings) {
CAF_CHECK_EQUAL(encode(0xb3, 0x7a, 0x4f, 0x2c, 0xc0, 0x62, 0x4f, 0x16, 0x90,
0xf6, 0x46, 0x06, 0xcf, 0x38, 0x59, 0x45, 0xb2, 0xbe,
0xc4, 0xea),
"s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
}
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include <vector> #include <vector>
#include "caf/all.hpp" #include "caf/all.hpp"
#include "caf/detail/encode_base64.hpp"
#include "caf/io/all.hpp" #include "caf/io/all.hpp"
using namespace caf; using namespace caf;
...@@ -36,38 +37,6 @@ using std::endl; ...@@ -36,38 +37,6 @@ using std::endl;
using std::string; using std::string;
using std::vector; using std::vector;
static constexpr const char base64_tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string encode_base64(const string& str) {
std::string result;
// consumes three characters from input
auto consume = [&](const char* i) {
int buf[]{
(i[0] & 0xfc) >> 2,
((i[0] & 0x03) << 4) + ((i[1] & 0xf0) >> 4),
((i[1] & 0x0f) << 2) + ((i[2] & 0xc0) >> 6),
i[2] & 0x3f,
};
for (auto x : buf)
result += base64_tbl[x];
};
// iterate string in chunks of three characters
auto i = str.begin();
for (; std::distance(i, str.end()) >= 3; i += 3)
consume(&(*i));
if (i != str.end()) {
// "fill" string with 0s
char cbuf[] = {0, 0, 0};
std::copy(i, str.end(), cbuf);
consume(cbuf);
// override filled characters (garbage) with '='
for (auto j = result.end() - (3 - (str.size() % 3)); j != result.end(); ++j)
*j = '=';
}
return result;
}
class host_desc { class host_desc {
public: public:
...@@ -117,7 +86,7 @@ bool run_ssh(actor_system& system, const string& wdir, const string& cmd, ...@@ -117,7 +86,7 @@ bool run_ssh(actor_system& system, const string& wdir, const string& cmd,
full_cmd += wdir; full_cmd += wdir;
full_cmd += '\n'; full_cmd += '\n';
full_cmd += cmd; full_cmd += cmd;
auto packed = encode_base64(full_cmd); auto packed = detail::encode_base64(full_cmd);
std::ostringstream oss; std::ostringstream oss;
oss << "ssh -Y -o ServerAliveInterval=60 " << host << R"( "echo )" << packed oss << "ssh -Y -o ServerAliveInterval=60 " << host << R"( "echo )" << packed
<< R"( | base64 --decode | /bin/sh")"; << R"( | base64 --decode | /bin/sh")";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment