ap6491
ap6491

Reputation: 835

Base64 decoding error

Using the Boost C++ library, I am trying to base64decode the following base64 encoded value: OTE4ZDUxYzM0ZTIyNmEzZDVmY2NjNjAyMzYyOTU5MTg0NzVmYWEwMjox using the following code:

std::string base64_decode(const std::string& s) {
  namespace bai = boost::archive::iterators;

  std::stringstream os;

  typedef bai::transform_width<bai::binary_from_base64<const char *>, 8, 6> base64_dec;

  unsigned int size = s.size();

  // Remove the padding characters, cf. https://svn.boost.org/trac/boost/ticket/5629
  if (size && s[size - 1] == '=') {
    --size;
    if (size && s[size - 1] == '=') --size;
  }
  if (size == 0) return std::string();

  std::copy(base64_dec(s.data()), base64_dec(s.data() + size),
            std::ostream_iterator<char>(os));

  return os.str();

Looks like the encoding happens correctly, however, while decoding, I still get the following error: terminate called after throwing an instance of boost::archive::iterators::dataflow_exception what(): attempt to decode a value not in base64 char set at the line:

std::copy(base64_dec(s.data()), base64_dec(s.data() + size), std::ostream_iterator<char>(os)); 

Upvotes: 1

Views: 794

Answers (2)

ap6491
ap6491

Reputation: 835

Changing the

std::copy(base64_dec(s.data()), base64_dec(s.data() + size), std::ostream_iterator<char>(os))

to

return std::string( base64_dec(s.c_str()), base64_dec(s.c_str() + size))

resolved the issue.

Upvotes: 0

Marcel Kr&#252;ger
Marcel Kr&#252;ger

Reputation: 909

I don't have the reputation to comment, so this is an answer instead:

"attemp to decode a value not in base64 char set" sounds like you should verify the actual input of the function, for the given input OTE4ZDUxYzM0ZTIyNmEzZDVmY2NjNjAyMzYyOTU5MTg0NzVmYWEwMjox your code works: https://ideone.com/zWl52N

Upvotes: 1

Related Questions