Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[base64] decoding input-buffers that have '\0' #12

Merged
merged 6 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions test/base64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#define BOOST_TEST_MODULE base64
#include <boost/test/included/unit_test.hpp>
#include "wasm_boost_test.hpp"

#include <xxhr/util.hpp>

std::map<std::string, std::string> test_data {
{ "", "" },
{ "TQ==" , "M" },
{ "TWE=" , "Ma" },
{ "TWFu" , "Man" },
{ "cGxlYXN1cmUu" , "pleasure." },
{ "bGVhc3VyZS4=" , "leasure." },
{ "ZWFzdXJlLg==" , "easure." },
{ "YXN1cmUu" , "asure." },
{ "c3VyZS4=" , "sure." },
{ "c3VyZS4=" , "sure." },
daminetreg marked this conversation as resolved.
Show resolved Hide resolved
{ "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);
}
}
9 changes: 9 additions & 0 deletions test/wasm_boost_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once


#ifdef __EMSCRIPTEN__
#include <signal.h>
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
8 changes: 5 additions & 3 deletions xxhr/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ namespace util {
inline std::string decode64(const std::string &val) {
using namespace boost::archive::iterators;
using It = transform_width<binary_from_base64<std::string::const_iterator>, 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<transform_width<std::string::const_iterator, 6, 8>>;
Expand Down