Skip to content

Commit

Permalink
Merge pull request #12 from nxxm/feature/fix-decoding-nullptr-ending-…
Browse files Browse the repository at this point in the history
…buffer

[base64] decoding input-buffers that have '\0'
  • Loading branch information
pysco68 committed Aug 26, 2021
2 parents b31af10 + fb72f7d commit ad85c4e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
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." },
{ "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

0 comments on commit ad85c4e

Please sign in to comment.