diff --git a/test/base64.cpp b/test/base64.cpp new file mode 100644 index 00000000..0d144839 --- /dev/null +++ b/test/base64.cpp @@ -0,0 +1,36 @@ +#define BOOST_TEST_MODULE base64 +#include +#include "wasm_boost_test.hpp" + +#include + +std::map test_data { + { "", "" }, + { "TQ==" , "M" }, + { "TWE=" , "Ma" }, + { "TWFu" , "Man" }, + { "cGxlYXN1cmUu" , "pleasure." }, + { "bGVhc3VyZS4=" , "leasure." }, + { "ZWFzdXJlLg==" , "easure." }, + { "YXN1cmUu" , "asure." }, + { "c3VyZS4=" , "sure." }, + { "c3VyZS4=" , "sure." }, + { "AQABAFEAAAD5AAAAAA==", { 0x01, 0x00, 0x01, 0x00, 0x51, 0x00, 0x00, 0x00, char(0xf9), 0x00, 0x00, 0x00, 0x00 } } +}; + + +BOOST_AUTO_TEST_CASE(decode) { + BOOST_REQUIRE(test_data.size() > 0); + for (const auto& data : test_data) { + BOOST_TEST_MESSAGE( data.first << " check if equals " << data.second); + BOOST_REQUIRE_EQUAL(xxhr::util::decode64(data.first), data.second); + } +} + +BOOST_AUTO_TEST_CASE(encode) { + BOOST_REQUIRE(test_data.size() > 0); + for (const auto& data : test_data) { + BOOST_TEST_MESSAGE( data.first << " check if equals " << data.second); + BOOST_REQUIRE_EQUAL(xxhr::util::encode64(data.second), data.first); + } +} diff --git a/test/wasm_boost_test.hpp b/test/wasm_boost_test.hpp new file mode 100644 index 00000000..ab617630 --- /dev/null +++ b/test/wasm_boost_test.hpp @@ -0,0 +1,9 @@ +#pragma once + + +#ifdef __EMSCRIPTEN__ + #include + extern "C" { + int sigaltstack(const stack_t *__restrict, stack_t *__restrict) { return 0; } //XXX: Check in newer emcc version if we can remove this. + } +#endif \ No newline at end of file diff --git a/xxhr/util.hpp b/xxhr/util.hpp index d0af9f5a..db86f608 100644 --- a/xxhr/util.hpp +++ b/xxhr/util.hpp @@ -168,11 +168,13 @@ namespace util { inline std::string decode64(const std::string &val) { using namespace boost::archive::iterators; using It = transform_width, 8, 6>; - return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))), [](char c) { - return c == '\0'; - }); + // See https://svn.boost.org/trac10/ticket/5629#comment:9 + // Boost binary_from_base64 transforms '=' into '\0', they need to be removed to support binary data + auto padding_count = std::count(val.end() - std::min(std::size_t{2}, val.size()), val.end() , '='); + return std::string(It(std::begin(val)), It(std::end(val) - padding_count)); } + inline std::string encode64(const std::string &val) { using namespace boost::archive::iterators; using It = base64_from_binary>;